fix(client): handle empty JSON response body without content-length#1998
Open
LeSingh1 wants to merge 1 commit into
Open
fix(client): handle empty JSON response body without content-length#1998LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
`defaultParseResponse` already returns `undefined` for an empty JSON response when the server sends `content-length: 0`, but when that header is absent (e.g. over HTTP/2 or with chunked transfer encoding) an empty body reached `response.json()` and threw `SyntaxError: Unexpected end of JSON input`. Read the body as text first and treat an empty payload the same as an explicit `content-length: 0`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
defaultParseResponse(src/internal/parse.ts) already special-cases an empty JSON response body:That guard only fires when the server sends an explicit
content-length: 0header. When the header is absent — which is common over HTTP/2, with chunked transfer encoding, or behind gateways/proxies and OpenAI-compatible servers — an empty body with a JSONcontent-typefalls through toresponse.json()and throws:So two byte-for-byte identical empty responses parse differently depending solely on whether the server happened to include
content-length: 0. The existing guard shows this case is meant to be handled gracefully; it's just incomplete.Reproduction
Fix
Read the body as text first and treat an empty payload the same as an explicit
content-length: 0. Non-empty bodies are still parsed withJSON.parse, so malformed JSON continues to throw as before.Verification
Added a
response parsingtest group intests/index.test.tscovering:content-length→ resolves toundefined(the fixed case; fails onmainwithSyntaxError: Unexpected end of JSON input)content-length: 0→ resolves toundefined(existing behavior preserved)npx tsc --noEmit,eslint, andprettier --checkall pass on the changed files.