Skip to content
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

Closed
vvo opened this issue Feb 1, 2021 · 7 comments · Fixed by #36186
Closed

res.json("string") is not working as expected #21749

vvo opened this issue Feb 1, 2021 · 7 comments · Fixed by #36186
Labels
kind: bug Confirmed bug that is on the backlog

Comments

@vvo
Copy link
Member

vvo commented Feb 1, 2021

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:

const result = "yes";
res.json(result);

Then the response body is:

yes

Which is not readable by a fetch call doing response.json().

instead of:

"yes"

Expected Behavior

I expect Next.js to send "yes" instead of yes down the wire when using res.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:

const isJSONLike = ['object', 'number', 'boolean'].includes(typeof body)
.

To Reproduce

backend route:

const result = "yes";
res.json(result);

frontend:

const response = await fetch("/api/call");
console.log(await response.json());

// Throw errors unexpected token
@vvo vvo added the bug Issue was opened via the bug report template. label Feb 1, 2021
@zEh-
Copy link

zEh- commented Feb 1, 2021

JSON has to be either of type object or array on root level. But throwing seems too harsh.

@vvo
Copy link
Member Author

vvo commented Feb 1, 2021

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:

image

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().

@zEh-
Copy link

zEh- commented Feb 1, 2021

Up to RFC 4627 only objects and arrays were valid JSON. Many tools still follow this rule.

@vvo vvo changed the title res.json("string") is not working res.json("string") is not working as expected Feb 3, 2021
@YuriGor
Copy link

YuriGor commented Mar 22, 2021

Another related issue - looks like it's impossible to send such a valid JSON value as null.
When I try to do res.json(null) e.g. if single data object not found - I get empty reply with status 200ok on frontend.

It's especially uncomfortable when using SWR, because in useSWR in this case I have undefined as result, but at the same time undefined supposed to indicate loading is still in progress, so I cannot distinguish empty result from pending state.

So please extend bug description to:
res.json("string") and res.json(null) are not working as expected
It would be good idea to check if all other JSON data types are also supported.

@YuriGor
Copy link

YuriGor commented Mar 22, 2021

as I found, SWR actually gives an error in these string/null cases, so it's only nextjs issue.

@anderlaw
Copy link

Another related issue - looks like it's impossible to send such a valid JSON value as null. When I try to do res.json(null) e.g. if single data object not found - I get empty reply with status 200ok on frontend.

It's especially uncomfortable when using SWR, because in useSWR in this case I have undefined as result, but at the same time undefined supposed to indicate loading is still in progress, so I cannot distinguish empty result from pending state.

So please extend bug description to: res.json("string") and res.json(null) are not working as expected It would be good idea to check if all other JSON data types are also supported.

good idea, and I encountered this error too

@github-actions
Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Confirmed bug that is on the backlog
Projects
None yet
5 participants