#Next JS 13.2 pages/api acting weird

1 messages · Page 1 of 1 (latest)

grizzled wigeon
#

I'm using Next 13.2 but for api routes I'm using pages/api

The problem is there's an api as putUserDetails it's basically a PUT req to a server but when I access it from one component I have to get body data in the file as JSON.parse(req.body) but when I'm accessing the api from another component it doesn't work with JSON.parse(req.body) it gives an error "SyntaxError: Unexpected end of JSON input" and I have to use req.body only. When I console.log(typeof req.body) it's string in both cases.

dawn shoal
#

but

#

The error "SyntaxError: Unexpected end of JSON input" typically occurs when you try to parse an invalid or incomplete JSON string using JSON.parse(). In your case, it seems that the JSON data you are trying to parse is incomplete or missing in the request body when you access the API from one of your components.

To troubleshoot the issue, you can try logging the entire req.body object to the console to see what data is being passed in the request body in both cases. You can also check the Content-Type header of the request to ensure that it is set to application/json and that the data being passed is indeed valid JSON.

If you find that the JSON data is missing or incomplete in one of the cases, you can check the code that is making the API request to ensure that it is passing the correct data. If the data being passed is not valid JSON, you may need to modify your API route to handle the data in a different format.

Alternatively, you can try using a package like body-parser to parse the request body automatically for you, instead of using JSON.parse(). This can simplify your code and ensure that the data is parsed correctly in all cases.

#

but would really recommend asking James or someone else....

frigid atlas
grizzled wigeon
#

sorry guys didn't reply I thought no one was gonna answer