Skip to content

fix(httpx): parse JSON bodies with unknown content length - #5880

Open
hainuoli wants to merge 2 commits into
zeromicro:masterfrom
hainuoli:codex/fix-httpx-unknown-content-length
Open

hainuoli wants to merge 2 commits into
zeromicro:masterfrom
hainuoli:codex/fix-httpx-unknown-content-length

Conversation

@hainuoli

@hainuoli hainuoli commented Nov 6, 2026

Copy link
Copy Markdown

This PR continues #5815 from my new account, @hainuoli. It preserves the same two commits and patch, including the follow-up addressing the original review. The original discussion remains in #5815.

Fixes #5820.

httpx.ParseJsonBody skips valid JSON bodies when ContentLength == -1, as received with HTTP/1.1 chunked requests. Accept unknown lengths and preserve the existing behavior for genuinely empty bodies.

For an unknown-length JSON body, read one byte through the existing bounded reader. An immediate EOF follows the empty-body mapping path, preserving optional fields, defaults, and required-field validation. Otherwise, replay the byte and continue JSON decoding. Other read errors are propagated, and whitespace-only bodies remain invalid JSON.

The parser's existing 8 MiB cap remains in place. The separately configured MaxBytesHandler limit is addressed in companion PR #5879, including HTTP 413 responses for read-time size errors in the default error handlers.

Regression tests cover non-empty unknown-length JSON, empty optional/default/required fields, whitespace-only bodies, and non-EOF read errors.

Validation for this migration:

  • go test -mod=readonly -race ./rest/httpx -count=1
  • go vet -mod=readonly ./rest/httpx
  • gofmt and git diff --check

The unchanged commit 2af395e96d67067c3f3be53996c4c6167e08dc54 passed upstream Go (Linux and Windows), CodeQL, staticcheck, and patch coverage checks in #5815. Those are historical results from the original PR; checks for this replacement PR must run separately and may require maintainer approval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: httpx.Parse skips JSON bodies with unknown content length

1 participant