GeezerVison is showing github.com — Fix JSON parsing error on empty response bodies without Content-Length header by Copilot · Pull Request #3201 · hey-api/hey-api
Conversation
Copilot
AI
commented
Jan 13, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fix SDK 'Unexpected end of JSON input' on empty response bodies
Summary
This PR fixes the issue where the SDK throws SyntaxError: Unexpected end of JSON input when the server returns an empty response body with status 200 (or other non-204 status codes) and no Content-Length header.
Changes
- Optimized JSON parsing: Read response as text once and parse with
JSON.parse() if non-empty, return {} for empty bodies
- Applied fix to all clients:
client-fetch, client-next, client-ky, and custom-client
- Added test coverage: Empty response scenarios without Content-Length header for all affected clients
- Updated snapshots: All test snapshots updated to reflect the new JSON parsing implementation
- Updated examples: All example projects regenerated with the new implementation
case 'json': {
// Some servers return 200 with no Content-Length and empty body.
// response.json() would throw; read as text and parse if non-empty.
const text = await response.text();
data = text ? JSON.parse(text) : {};
break;
}
The fix preserves existing behavior for all valid responses while gracefully handling the edge case of empty bodies with non-204 status codes. This approach reads the response body only once, making it more efficient than using response.clone().text() followed by response.json().
Why not ofetch, nuxt, and axios?
- ofetch: Already has a similar fix implemented (uses
response.clone().text() because ofetch populates _data on the response object and needs to preserve raw response access)
- nuxt: Uses
$fetch internally which is based on ofetch, so inherits the fix
- axios: Uses axios library which handles JSON parsing automatically through its response interceptors and returns parsed
data from the response object
Original prompt
This section details on the original issue you should resolve
<issue_title>SDK throws 'Unexpected end of JSON input' on empty response bodies</issue_title>
<issue_description>See the issue in anomalyco/opencode#7889 for more context and reproduction steps.</issue_description>
<agent_instructions>Reproduce the issue first using the details provided in the other GitHub issue link, and produce fix afterwards.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
vercel
Bot
commented
Jan 13, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Copilot
AI
changed the title
[WIP] Fix SDK for unexpected end of JSON input error
Fix JSON parsing error on empty response bodies without Content-Length header
Jan 13, 2026
|
@copilot why not do this?
const text = await response.text();
const data = text ? JSON.parse(text) : null;
Wouldn't that be better as we don't read the body twice? And can you explain why this fix isn't needed for the ofetch, nuxt, and axios clients?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@copilot why not do this?
...
You're absolutely right! Changed to use response.text() + JSON.parse() instead of reading the body twice. This is more efficient and cleaner.
Why ofetch uses clone(): ofetch populates a _data property on the response object, so it needs to preserve the raw response. For fetch/ky/next clients, we don't have that constraint.
Why not axios/nuxt:
- axios: Handles JSON parsing automatically via its response interceptors and returns parsed
data
- nuxt: Uses
$fetch which is based on ofetch internally
Commit: 7bcbb60
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@copilot update snapshots with pnpm test:update and examples with pnpm examples:generate and commit the changes please
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@copilot update snapshots with pnpm test:update and examples with pnpm examples:generate and commit the changes please
Updated snapshots and examples.
Commit: d6a8060
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Fix JSON parsing error on empty response bodies without Content-Length header for the @hey-api/client-fetch plugin.
codecov
Bot
commented
Jan 13, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 33.47%. Comparing base (6b337ae) to head (8428ca5).
⚠️ Report is 9 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #3201 +/- ##
==========================================
+ Coverage 33.40% 33.47% +0.07%
==========================================
Files 451 451
Lines 34484 34496 +12
Branches 2229 2244 +15
==========================================
+ Hits 11518 11547 +29
+ Misses 22936 22919 -17
Partials 30 30
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.
🚀 New features to boost your workflow:
- ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Fixes a JSON parsing error when handling empty response bodies that lack a Content-Length header.
Fixes a JSON parsing error when handling empty response bodies that lack a Content-Length header.
|
Open in StackBlitz
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/codegen-core@3201
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@3201
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@3201
npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@3201
commit: 8428ca5
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
IAluI
pushed a commit
to IAluI/openapi-ts
that referenced
this pull request
Sep 26, 2026
…se-json-issue
Fix JSON parsing error on empty response bodies without Content-Length header
This branch was successfully deployed
1 active
deployment
Reviewers
1 more reviewer
mrlubos
mrlubos approved these changes
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Labels
bug 🔥
Broken or incorrect behavior.
lgtm
This PR has been approved by a maintainer
size:L
This PR changes 100-499 lines, ignoring generated files.
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|