Skip to content

hc5: do not send Content-Length 0 for bodyless GET and HEAD - #3693

Merged
velo merged 1 commit into
OpenFeign:masterfrom
Inf1mum:fix/2085-get-no-content-length
Nov 16, 2026
Merged

velo merged 1 commit into
OpenFeign:masterfrom
Inf1mum:fix/2085-get-no-content-length

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Summary

The issue reports that ApacheHttp5Client sends a Content-Length: 0 header for GET requests with an empty body, which picky load balancers (e.g. some ALB configurations) reject.

Root cause: ApacheHttp5Client#toClassicHttpRequest wrapped an absent or empty body in an empty ByteArrayEntity (the else branch at the old line 180), which made Apache HttpClient advertise Content-Length: 0 on 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-Length header is sent. Everything else is unchanged:

  • A GET with a non-empty body (unusual but possible via Request.Body) still keeps its entity (asserted by the new getWithBodyKeepsTheEntity test).
  • Methods that can carry a body keep the explicit empty entity, so an intentionally empty POST/PUT/PATCH still sends Content-Length: 0. This is a deliberate non-regression constraint: the existing patch() test in AbstractClientTest (asserts Content-Length: 0 for an empty PATCH) stays green, and the new postWithEmptyBodySendsContentLengthZero test pins the behavior for POST.

The helper mirrors the canOmitEmptyEntity pattern 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 carry Content-Length (the issue scenario).
  • getWithEmptyBodyDoesNotSendContentLength — GET with an explicitly empty body must not carry Content-Length.
  • headWithoutBodyDoesNotSendContentLength — HEAD, like GET, must not carry Content-Length.
  • getWithBodyKeepsTheEntity — a GET with a non-empty body keeps its entity (via the package-private toClassicHttpRequest, since MockWebServer chokes on GET requests with a body).
  • postWithEmptyBodySendsContentLengthZero — an intentionally empty POST still sends Content-Length: 0.

Additionally verified with a standalone raw-socket harness against the patched client, capturing the exact request bytes on the wire:

GET  (null body)   -> no Content-Length header
GET  (empty body)  -> no Content-Length header
HEAD (null body)   -> no Content-Length header
POST (empty body)  -> Content-Length: 0
PATCH (empty body) -> Content-Length: 0
PUT  (empty body)  -> Content-Length: 0
GET  (body=foo)    -> Content-Length: 3 + body transmitted

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). testCompile is 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.
  • Touched classes are javac-clean: ApacheHttp5Client.java compiled by the run above, ApacheHttp5ClientTest.java compiled standalone with javac 21 (no warnings).
  • Because the maven test phase could not run locally, the new tests plus the whole inherited AbstractClientTest suite (37 tests) were executed with the JUnit Console Launcher on JDK 21 against the patched classes: 37/37 passed, including patch() (empty PATCH keeps Content-Length: 0).
  • Raw-socket harness results above were produced against the patched ApacheHttp5Client.

Note on the needs info label

The issue is currently labeled needs info. A collaborator could not reproduce it on a newer stack; we found the likely explanation: current httpclient5 5.6.5 suppresses the Content-Length header 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

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 velo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - no Content-Length 0 for bodyless GET/HEAD is the right fix. Tests pass and this restores correct HTTP behavior.

@velo
velo enabled auto-merge (squash) November 16, 2026 10:55
@velo
velo merged commit ed9a6c0 into OpenFeign:master Nov 16, 2026
3 checks passed
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.

GET request with empty body has Content-Length header with 0

2 participants