Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8276996

Add an HttpRequest.Builder.HEAD method to build a HEAD request.

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 18
    • core-libs
    • None
    • source, binary
    • minimal
    • Hide
      It's possible that there might be existing implementations or derived interfaces of `java.net.http.HttpRequest.Builder` out there which might already have a `HEAD()` method with either the same return type or a different one and potentially a different semantic expectation. This new method being introduced in this interface can potentially cause an issue in such existing classes/interfaces. The chances of such classes/interfaces being there are relatively low (this is mostly a guess given how relatively new this API is)
      Show
      It's possible that there might be existing implementations or derived interfaces of `java.net.http.HttpRequest.Builder` out there which might already have a `HEAD()` method with either the same return type or a different one and potentially a different semantic expectation. This new method being introduced in this interface can potentially cause an issue in such existing classes/interfaces. The chances of such classes/interfaces being there are relatively low (this is mostly a guess given how relatively new this API is)
    • Java API
    • SE

      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());
      +        }
      +
      

            jpai Jaikiran Pai
            dfuchs Daniel Fuchs
            Chris Hegarty, Daniel Fuchs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: