Skip to content

BufferingClientHttpRequestFactory should not set content length for empty request body #32650

@nhmarujo

Description

@nhmarujo

We were recently moving from WebClient to RestClient and one of the 3rd parties we deal with started returning us:
HTTP Error 411. The request must be chunked or have a content length

The given request is a POST without any body and looking at the outgoing requests the only difference between before and after is that now the request we generate has a Content-Length: 0 while before that header was absent.

I do concede that I'm not sure that the 3rd party is handling this properly, but I also have to say that I don't see why the framework would be setting Content-Length: 0 when there is no body.

I was looking deeper into the code and I can see that the decision to add the Content-Length header is being taken here and that happens because headers.getContentLength() returns -1. Looking further at the logic on HttpHeaders I can see here that when that header is not set it just returns -1, which is happening in my case.

In my view I don't think it makes sense to send this header in this case, so I was wondering if that could be fixed, perhaps by just doing instead:

if (bytes.length > 0 && headers.getContentLength() < 0) {
	headers.setContentLength(bytes.length);
}

This should still ensure that the header is added if it is missing and there is a body, but will not add it if there is no body. It will also not override if explicitly set.

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)status: declinedA suggestion or change that we don't feel we should currently apply

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions