Created
July 10, 2023 02:28
-
-
Save toshvelaga/aedb1f8a749a2f9e3844e59c7f2ff1ff to your computer and use it in GitHub Desktop.
NEXT JS POST REQUEST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | |
import { NextResponse } from 'next/server' | |
export const runtime = 'edge' | |
export async function POST(req, res) { | |
const body = await req.json() | |
const { text, model } = body | |
const data = { | |
text, | |
model, | |
} | |
const requestOptions = { | |
method: 'POST', | |
body: JSON.stringify(data), | |
} | |
console.log(data) | |
const answer = await fetch( | |
URL_HERE | |
) | |
.then((response) => { | |
if (!response.ok) { | |
throw new Error('Network response was not ok') | |
} | |
return response.json() | |
}) | |
.then((data) => { | |
console.log(data) | |
return data | |
}) | |
.catch((error) => { | |
console.error(error) | |
}) | |
console.log('answer: ', answer) | |
if (!answer) { | |
return NextResponse.json({ | |
answer: `An unexpected error occurred. Please email toshvelaga@gmail.com`, | |
}) | |
} else | |
return NextResponse.json({ | |
answer, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment