Skip to content

Instantly share code, notes, and snippets.

@toshvelaga
Created July 10, 2023 02:28
Show Gist options
  • Save toshvelaga/aedb1f8a749a2f9e3844e59c7f2ff1ff to your computer and use it in GitHub Desktop.
Save toshvelaga/aedb1f8a749a2f9e3844e59c7f2ff1ff to your computer and use it in GitHub Desktop.
NEXT JS POST REQUEST
// 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