JDK-8276996 : Add an HttpRequest.Builder.HEAD method to build a HEAD request.
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.net
  • Priority: P4
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 18
  • Submitted: 2021-11-11
  • Updated: 2021-11-15
  • Resolved: 2021-11-15
Related Reports
CSR :  
Description
Summary
-------

A new `public Builder HEAD()` method is introduced on the
`java.net.http.HttpRequest.Builder` interface.

Problem
-------

The HTTPClient APIs in the java.net.http module allows applications
to create a HTTP client and issue HTTP requests. The 
`java.net.http.HttpRequest.Builder` interface has APIs which allow
applications to create such requests. Currently the 
`java.net.http.HttpRequest.Builder` has convenience methods like
`GET()`, `POST()`, `DELETE()` and such. However, it is missing a
convenience method for creating a `HEAD` request. Introducing a
`HEAD()` method will make it consistent with these other available
methods and make it convenient for application use.

Solution
--------

Since the `java.net.http.HttpRequest.Builder` is a public interface
in a public module, the new HEAD() method will be introduced as a
"default" method. The default implementation expectation is that 
it will call the "method(String method, BodyPublisher bodyPublisher)"
API that currently exists on the Builder interface, by passing it "HEAD" and
`BodyPublishers.noBody()` as the parameters and return back whatever that
call returns.


Specification
-------------

```

diff --git a/src/java.net.http/share/classes/java/net/http/HttpRequest.java b/src/java.net.http/share/classes/java/net/http/HttpRequest.java
index 5dc0486826a..59174783263 100644
--- a/src/java.net.http/share/classes/java/net/http/HttpRequest.java
+++ b/src/java.net.http/share/classes/java/net/http/HttpRequest.java 
+        /**
+         * Sets the request method of this builder to HEAD.
+         *
+         * @implSpec The default implementation is expected to have the same behaviour as:
+         * {@code return method("HEAD", BodyPublishers.noBody());}
+         *
+         * @return this builder
+         * @since 18
+         */
+        default Builder HEAD() {
+            return method("HEAD", BodyPublishers.noBody());
+        }
+

```





Comments
Moving to Approved.
15-11-2021

Thank you for the review, Daniel. Moving to "Finalized".
12-11-2021