Skip to content

HttpComponentsClientHttpRequestFactory does not set Content-Length: 0 #32678

@tholinka

Description

@tholinka

Affects: 6.1.6

I have an API that requires sending a Content-Length: 0 when the body is null/empty.

When we previously upgraded to Spring Boot 3 / Spring 6, we changed our RestTemplate to use new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory()) as the request factory in order to ensure that a Content-Length was set.

This worked on 6.1.5, but on upgrading to 6.1.6 we are no longer sending the Content-Length header.


Stepping through the code with a debugger, this changed in 019ce44 / gh-32612.

I can force the old behavior if I do the following, which leads me to believe it is that commit (effectively, I'm trying to just skip over the new if):

  • Set a breakpoint at line 58 and at line 59 of BufferingClientHttpRequestWrapper
  • (on line 58 breakpoint) in the evaluation expression window run bufferedOutput = new byte[1];
  • Continue to line 59 breakpoint
  • (on line 59 breakpoint) in the evaluation expression window run bufferedOutput = new byte[0];

Doing this then causes the Content-Length: 0 header to be sent as expected.


I tried also manually setting Content-Length: 0 as a header, but HttpComponentsClientHttpRequest does not allow that, as is explicitly ignores Content-Length as a header.

else if (!HttpHeaders.CONTENT_LENGTH.equalsIgnoreCase(headerName) &&


This still works as expected (Content-Length: 0 is set) when using SimpleClientHttpRequestFactory, so for now I can work around this by using new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()) instead. However, I would prefer to use HttpComponentsClientHttpRequestFactory instead of Simple if possible.

Metadata

Metadata

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions