Skip to content

SyntaxError: Unexpected end of JSON input #1284

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

Closed
1 task
Rykuno opened this issue Aug 9, 2023 · 11 comments
Closed
1 task

SyntaxError: Unexpected end of JSON input #1284

Rykuno opened this issue Aug 9, 2023 · 11 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library stale

Comments

@Rykuno
Copy link

Rykuno commented Aug 9, 2023

Looks like #1280 might fix this when merged.

Description
With the update to 0.7.1 from 0.6.0, errors began to throw with SyntaxError: Unexpected end of JSON input when a record from the API returned null/undefined.

A brief description of the bug. Provide either a screenshot or the full error message

web:dev: SyntaxError: Unexpected end of JSON input
web:dev:     at JSON.parse (<anonymous>)

Reproduction
How can this be reproduced / when did the error occur? Does the issue occur in a specific browser, or all browsers?

Expected result
Expect null/undefined rather than failing to parse json

(in case it’s not obvious)

Checklist

@Rykuno Rykuno added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Aug 9, 2023
@drwpow
Copy link
Contributor

drwpow commented Aug 9, 2023

#1280 is unrelated and is on the typegen side; that won’t affect a runtime error like what you’re seeing.

So I’m assuming that your API does NOT return a 204 No content status, but instead, returns a 200 where the top-level response may be null. In that case, I’m not sure if we handle that. I guess this is a bug? But this is a curious API choice to return 200 with a null or undefined response at the root level. But technically not invalid.

@drwpow
Copy link
Contributor

drwpow commented Aug 9, 2023

It should also be noted that we respect the Content-Length header, so if your API sent "Content-Length": "0" then we wouldn’t try and parse JSON in that instance, either.

@Rykuno
Copy link
Author

Rykuno commented Aug 9, 2023

We have this issue in two areas w/ the new version. The issue is this is happening for responses from two different very popular public API's; so we have no control over the implementation :(

@tuler
Copy link

tuler commented Sep 6, 2023

I have a 202 response with Content-Type: text/plain and Content-Length: 27, which is a message.
And I'm getting a JSON parse error as well.

@KadoBOT
Copy link

KadoBOT commented Sep 15, 2023

got the same error:

Error: 26 |         // parse response (falling back to .text() when necessary)
27 |         if (response.ok) {
28 |             let data = response.body;
29 |             if (parseAs !== "stream") {
30 |                 const cloned = response.clone();
31 |                 data = typeof cloned[parseAs] === "function" ? await cloned[parseAs]() : await cloned.text();
                                                                         ^
SyntaxError: Unexpected end of JSON input

@StagnantIce
Copy link

Get the same error on last version for PATCH method with empty body

So if I send body: {} its works fine, but i have red line under because generated API not expected. (Response return 204)
If I remove body: {} its display me Unexpected end of JSON input (Response return 400)

@StagnantIce
Copy link

Another way to fix it add

@ApiBody({ schema: {} })

and send body: {}. But I m not sure that its correct way.

@yethee
Copy link

yethee commented May 29, 2024

I faced with this issue when API server returns a response with status 202 and there is neither a body nor content-length header. As a workaround, I override the parse strategy (parseAs: 'stream') for a specific method to avoid the issue.

@brandonp-ais
Copy link

brandonp-ais commented Jun 17, 2024

Hi, I am also experiencing this issue with a response status 202.

It should also be noted that we respect the Content-Length header, so if your API sent "Content-Length": "0" then we wouldn’t try and parse JSON in that instance, either.

We looked at this as a solution to the problem, but unfortunately it will not work as our backend API is incapable of doing this without significant memory increases.

https://github.com/spring-projects/spring-framework/wiki/Upgrading-to-Spring-Framework-6.x#web-applications

To reduce memory usage in RestClient and RestTemplate, most ClientHttpRequestFactory
implementations no longer buffer request bodies before sending them to the server. As a result, for certain content types such as JSON, the contents size is no longer known, and a Content-Length header is no longer set.

So this means that adding the header is effectively removed by SpringBoot.

Copy link
Contributor

This issue is stale because it has been open for 90 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

@github-actions github-actions bot added the stale label Sep 15, 2024
Copy link
Contributor

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library stale
Projects
None yet
Development

No branches or pull requests

7 participants