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

Unexpected end of JSON input with empty response #1977

Open
1 task done
SandroMaglione opened this issue Oct 30, 2024 · 1 comment · May be fixed by #1986
Open
1 task done

Unexpected end of JSON input with empty response #1977

SandroMaglione opened this issue Oct 30, 2024 · 1 comment · May be fixed by #1986
Assignees
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@SandroMaglione
Copy link

Description

When a response has an empty body, the client fails with a JSON.parse error:

SyntaxError: Unexpected end of JSON input
      at JSON.parse (<anonymous>)
      at parseJSONFromBytes (node:internal/deps/undici/undici:4747:19)
      at successSteps (node:internal/deps/undici/undici:4718:27)
      at specConsumeBody (node:internal/deps/undici/undici:4724:9)
      at HttpResponse.json (node:internal/deps/undici/undici:4614:18)

This error happens here. response[parseAs]() fails to convert the empty response to json and crashes, never reaching the fallback.

Expected result

The client should be able to handle empty responses without crashing with JSON.parse errors.

Checklist

@SandroMaglione SandroMaglione added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Oct 30, 2024
@DjordyKoert DjordyKoert linked a pull request Nov 5, 2024 that will close this issue
3 tasks
@drwpow drwpow self-assigned this Nov 27, 2024
@ViktorPontinen
Copy link

We've had this issue as well. Generally you want your API to return 204 status if there is no body, which would make open-api skip the json parse and avoid crash.

In our case, we had PUT requests that returned empty body by design, and returned 201 status. For those requests you can add parseAs: 'stream' option and it should skip the json parse as well.

For cases where you sometimes get response, I have no idea how to resolve.

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
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants