Use HTTP methods in JdkClientHttpRequest when possible

Prior to this commit, we would use the
`java.net.http.HttpRequest.Builder#method(String, BodyPublisher)` to
create HTTP requests for the JDK HttpClient. This method requires a
non-null body publisher; providing an empty publisher writes a
"Content-Length: 0" header to all requests.

As of Java 19, this behavior changes for `HttpRequest.Builder#GET` and
similar methods, where the body publisher is considered as null and no
"Content-Length" header is written.

This commit aligns with this behavior and favors dedicated HTTP methods
whenever available.`

Closes gh-34971
This commit is contained in:
Brian Clozel
2025-06-02 15:36:20 +02:00
parent c2d678879f
commit 659472f9e3
2 changed files with 33 additions and 1 deletions
@@ -149,7 +149,16 @@ class JdkClientHttpRequest extends AbstractStreamingClientHttpRequest {
}
});
builder.method(this.method.name(), bodyPublisher(headers, body));
switch (this.method.name()) {
case "GET" :
builder.GET();
break;
case "DELETE" :
builder.DELETE();
break;
default :
builder.method(this.method.name(), bodyPublisher(headers, body));
}
return builder.build();
}