RFR: 8283544: HttpClient GET method adds Content-Length: 0 header

Daniel Fuchs dfuchs at openjdk.java.net
Tue Mar 29 19:46:41 UTC 2022


On Tue, 29 Mar 2022 18:31:02 GMT, Daniel Jeliński <djelinski at openjdk.org> wrote:

>> **Issue**
>> When using the `HttpClient.send()` to send a GET request created using the `HttpRequest.newBuilder()`, a `Content-length: 0` header is set. This behaviour causes issues with many services as a body related header is usually not expected to be included with a GET request. 
>> 
>> **Solution**
>> `Http1Request.java` was modified so that when the request method is a GET, a `Content-length` header is not added to the request. However, if a developer chooses to include a body in a GET request (though it is generally considered bad practice), a `Content-length` header with the appropriate value will be added.
>
> src/java.net.http/share/classes/jdk/internal/net/http/Http1Request.java line 302:
> 
>> 300: 
>> 301:         // GET with no body should not set the Content-Length header
>> 302:         if (requestPublisher != null || !"GET".equals(request.method())) {
> 
> Can we remove the check for "GET"? This way we will let the users decide if they want to send content-length or not, regardless of the chosen request method.

Practically that would mean not sending Content-Length: 0 by default for GET, DELETE, and HEAD. All other requests methods would have either a Content-Length or Transfer-Encoding. I suspect that HEAD should probably be handled the same way than GET. But should DELETE not have a body? I know that some servers will balk if DELETE has a body. But would they expect Content-Length: 0 not to be included? @Michael-Mc-Mahon what do you think?

-------------

PR: https://git.openjdk.java.net/jdk/pull/8017


More information about the net-dev mailing list