fix: ensure HTTP/2 sends Content-Length for empty POST requests #4613
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.
This relates to...
Fixes #4594
Related to #4612
Rationale
This PR addresses the discrepancy in behavior between HTTP/1 and HTTP/2 when handling empty POST requests.
According to RFC 9110:
Previously:
Content-Length: 0
Content-Length
header entirelyThis inconsistency causes compatibility issues with servers like Internet Information Server (IIS), which reject empty POST requests without a Content-Length header, returning
411 Length Required
.Changes
Implementation (
lib/dispatcher/client-h2.js
)Content-Length
when bothcontentLength === 0
AND the method doesn't expect a payload (like GET, HEAD)Content-Length: 0
is now sent even when the body is emptycontentLength === 0
without a bodyTests (
test/fetch/http2.js
)Content-Length: 0
Test Results
All HTTP/2 tests pass (51 tests total):
[Fetch] Empty POST without h2 has Content-Length
[Fetch] Empty POST with h2 has Content-Length
Breaking Changes and Deprecations
None. This is a bug fix that aligns behavior with RFC 9110 recommendations and HTTP/1 implementation.
Status
🤖 Generated with Claude Code