Fix JSON parsing error on empty response bodies without Content-Length header#3201
Fix JSON parsing error on empty response bodies without Content-Length header#3201
Conversation
|
|
🦋 Changeset detectedLatest commit: 8428ca5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
…ON input' error Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
|
@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? |
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
... You're absolutely right! Changed to use Why ofetch uses Why not axios/nuxt:
Commit: 7bcbb60 |
|
@copilot update snapshots with |
Co-authored-by: mrlubos <12529395+mrlubos@users.noreply.github.com>
Fix JSON parsing error on empty response bodies without Content-Length header for the @hey-api/client-fetch plugin.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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.
commit: |
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 inputwhen the server returns an empty response body with status 200 (or other non-204 status codes) and noContent-Lengthheader.Changes
JSON.parse()if non-empty, return{}for empty bodiesclient-fetch,client-next,client-ky, andcustom-clientThe 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 byresponse.json().Why not ofetch, nuxt, and axios?
response.clone().text()because ofetch populates_dataon the response object and needs to preserve raw response access)$fetchinternally which is based on ofetch, so inherits the fixdatafrom the response objectOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.