Repository navigation
hc5: do not send Content-Length 0 for bodyless GET and HEAD - #3693
Merged
Merged
Conversation
GET and HEAD are bodyless by RFC 7231 semantics. ApacheHttp5Client wrapped an absent or empty body in an empty ByteArrayEntity, which made Apache HttpClient advertise 'Content-Length: 0' on the wire. Picky load balancers (e.g. some ALB configurations) reject such requests. When the method is GET or HEAD and the body is absent or empty, no entity is set anymore, so no Content-Length header is sent. Requests with a non-empty body are unchanged. Methods that can carry a body keep the explicit empty entity, so an intentionally empty POST, PUT or PATCH still sends 'Content-Length: 0' (covered by the existing patch() test in AbstractClientTest and a new postWithEmptyBodySendsContentLengthZero test). The regression tests live in ApacheHttp5ClientTest: a GET without a body, a GET with an explicitly empty body and a HEAD request must not carry Content-Length, while a GET with a body keeps its entity. Fixes OpenFeign#2085 Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>
velo
approved these changes
Nov 16, 2026
velo
left a comment
Member
There was a problem hiding this comment.
LGTM - no Content-Length 0 for bodyless GET/HEAD is the right fix. Tests pass and this restores correct HTTP behavior.
velo
enabled auto-merge (squash)
November 16, 2026 10:55
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.
Summary
The issue reports that
ApacheHttp5Clientsends aContent-Length: 0header for GET requests with an empty body, which picky load balancers (e.g. some ALB configurations) reject.Root cause:
ApacheHttp5Client#toClassicHttpRequestwrapped an absent or empty body in an emptyByteArrayEntity(theelsebranch at the old line 180), which made Apache HttpClient advertiseContent-Length: 0on the wire.Fix
For GET and HEAD (bodyless by RFC 7231 semantics) with an absent or empty body, no entity is set anymore, so no
Content-Lengthheader is sent. Everything else is unchanged:Request.Body) still keeps its entity (asserted by the newgetWithBodyKeepsTheEntitytest).Content-Length: 0. This is a deliberate non-regression constraint: the existingpatch()test inAbstractClientTest(assertsContent-Length: 0for an empty PATCH) stays green, and the newpostWithEmptyBodySendsContentLengthZerotest pins the behavior for POST.The helper mirrors the
canOmitEmptyEntitypattern already used by the Apache HTTP 4 client (#3634), restricted to GET and HEAD.Reproducer / regression tests
New tests in
ApacheHttp5ClientTest:getWithoutBodyDoesNotSendContentLength— GET with a null body must not carryContent-Length(the issue scenario).getWithEmptyBodyDoesNotSendContentLength— GET with an explicitly empty body must not carryContent-Length.headWithoutBodyDoesNotSendContentLength— HEAD, like GET, must not carryContent-Length.getWithBodyKeepsTheEntity— a GET with a non-empty body keeps its entity (via the package-privatetoClassicHttpRequest, since MockWebServer chokes on GET requests with a body).postWithEmptyBodySendsContentLengthZero— an intentionally empty POST still sendsContent-Length: 0.Additionally verified with a standalone raw-socket harness against the patched client, capturing the exact request bytes on the wire:
Verification (honest split)
mvn -pl hc5 -Pdev test -Dtoolchain.skip=true -Dlicense.skip=true: the hc5 main sources compile on JDK 21 (this machine has no JDK 25).testCompileis pinned to source/target 25 in the root pom, so the test sources did not compile locally — that is expected and documented here; CI on JDK 25 is the gate for the tests.ApacheHttp5Client.javacompiled by the run above,ApacheHttp5ClientTest.javacompiled standalone with javac 21 (no warnings).AbstractClientTestsuite (37 tests) were executed with the JUnit Console Launcher on JDK 21 against the patched classes: 37/37 passed, includingpatch()(empty PATCH keepsContent-Length: 0).ApacheHttp5Client.Note on the
needs infolabelThe issue is currently labeled
needs info. A collaborator could not reproduce it on a newer stack; we found the likely explanation: currenthttpclient55.6.5 suppresses theContent-Lengthheader itself for GET/HEAD requests carrying an empty entity, so the wire symptom is hidden on recent versions (the reporter used httpclient5 5.2.1). The feign-side code still wrapped empty bodies in entities, so the behavior depended on the HttpClient version. This change makes the omission explicit and version-independent, which should let the issue be closed; the reproducer above documents the scenario.Fixes #2085