Transitioning from Next.js 12 to 13 has been quite perplexing, especially when it comes to handling JSON data. Every time I attempt a fetch request, I find myself needing to refer back to documentation due to the confusion surrounding JSON. Is there anyone out there who can assist me in clearing up this issue once and for all?
Here is an example of how I make a fetch POST request to the API handler:
//client component
const res = await fetch('/api/dostuff', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({dataObj: {hey: "ho"})
})
//I NEED HELP IN RECEIVING THE RESPONSE
And here is how I attempt to process the response:
//route.js in api/dostuff/
export async function POST(request) {
const { dataObj } = await request.json()
//CAN SOMEONE HELP ME WITH RETURNING THIS
}
I have experimented with various ways of handling JSON data, including JSON.stringify
, JSON.parse
, and json()
, but none seem to be effective. It is frustrating to encounter such complexity after not having to deal with these issues in Next.js 12.