spring-framework: HttpComponentsClientHttpRequestFactory does not set Content-Length: 0
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 019ce4418cd57391ed61c7fb6812bbd527db35e6 / gh-32612. https://github.com/spring-projects/spring-framework/blob/019ce4418cd57391ed61c7fb6812bbd527db35e6/spring-web/src/main/java/org/springframework/http/client/BufferingClientHttpRequestWrapper.java#L58
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. https://github.com/spring-projects/spring-framework/blob/019ce4418cd57391ed61c7fb6812bbd527db35e6/spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequest.java#L110
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.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 4
- Comments: 19 (6 by maintainers)
The argument in https://stackoverflow.com/a/78182957 assumes that a client is free to choose whether to include the
Content-Lengthheader or not in POST requests. One can argue that this is indeed true because only SHOULD instead of MUST is used in “A user agent SHOULD send Content-Length in a request when the method defines a meaning for enclosed content and it is not sending Transfer-Encoding.” even if one believes that all POST requests are meant with “the method defines a meaning for enclosed content”.I’m not convinced that it would be a good idea to actually implement a POST resource which behaves differently for absent and empty requests. But I’m fine with settling on: The HTTP RFC allows that.
The question remains how Spring should behave. I agree with https://github.com/spring-projects/spring-framework/issues/32678#issuecomment-2067090473 which expects a
Content-Length: 0header being present for:I think, it would be pragmatic to restore this behavior while simultaneously allowing people to differentiate between absent and empty request bodies by having Spring exclude the
Content-Lengthheader for:I, too, suffer from this regression because a legacy system which is called by a service I’m helping to maintain requires
Content-Length: 0for empty POST requests.I think, for empty POST/PUT/PATCH requests, the Content-Length header should be set to 0. This is supported by the HTTP RFC#9110. It says:
Note that, „when the method defines a meaning for enclosed content“ in the first sentence refers to e.g. POST/PUT/PATCH requests. And „the method semantics do not anticipate such data“ in the last sentence refers to e.g. GET requests.
PS: We can also dig into the history of the HTTP RFCs.
For instance, RFC#2616 said about the
Content-Lengthheader:And section 4.4 said:
We have discussed this as a team and we think that the issue might not be in the
BufferingClientHttpRequestFactory, but a bug in our implementation of the HttpComponents client. In theHttpComponentsClientHttpRequest, we should probably set aNullEntityinstead of skipping it entirely. This should result in setting the “Content-Length” header and align the behavior with other clients.FYI we just reopened #32650 and we might revert the change there.
Sadly this isn’t changeable from my team. We’re trying to call an external API that is very resistant to change, and they’ve always required sending
Content-Lengthas a header.e.g. this curl works:
and so does this one:
but this one does not:
So having some way to send
Content-Lengthwhen it’s 0 would be nice. From my understanding of the spec, it’s just negative numbers that aren’t valid. 0 is a validContent-Length, I believe.