New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
res.json("string") is not working as expected #21749
Comments
JSON has to be either of type object or array on root level. But throwing seems too harsh. |
Hi @zEh-, when you say JSON has to be either object or array on root level, is this some Next.js requirement written somewhere? I have never seen that. It seems Next.js is fine with JSON being either object, number or boolean, but not string. While string is valid JSON, see: Thanks! PS: The throw happens on the frontend, when trying to read the response from the backend, which is not well-written JSON, even if I used .json(). |
Up to RFC 4627 only objects and arrays were valid JSON. Many tools still follow this rule. |
Another related issue - looks like it's impossible to send such a valid JSON value as It's especially uncomfortable when using SWR, because in useSWR in this case I have So please extend bug description to: |
as I found, SWR actually gives an error in these string/null cases, so it's only nextjs issue. |
good idea, and I encountered this error too |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
10.0.6
What version of Node.js are you using?
12.20.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
When using:
Then the response body is:
Which is not readable by a fetch call doing response.json().
instead of:
Expected Behavior
I expect Next.js to send
"yes"
instead ofyes
down the wire when usingres.json("yes")
."yes"
is valid JSON and useful when all you need from your server is a single response, not an object.When using
res.json(data)
, I expect Next.js to just do:res.send(JSON.stringify(data));
, while right now it's trying to be smarter than that:next.js/packages/next/next-server/server/api-utils.ts
Line 249 in 004ad62
To Reproduce
backend route:
frontend:
The text was updated successfully, but these errors were encountered: