Class HttpRequest

  • public abstract class HttpRequest
    extends Object
    An HTTP request.

    An HttpRequest instance is built through an HttpRequest builder. An HttpRequest builder is obtained from one of the newBuilder methods. A request's URI, headers, and body can be set. Request bodies are provided through a BodyPublisher supplied to one of the POST, PUT or method methods. Once all required parameters have been set in the builder, build will return the HttpRequest. Builders can be copied and modified many times in order to build multiple related requests that differ in some parameters.

    The following is an example of a GET request that prints the response body as a String:

       HttpClient client = HttpClient.newHttpClient();
       HttpRequest request = HttpRequest.newBuilder()
       client.sendAsync(request, BodyHandlers.ofString())

    The class BodyPublishers provides implementations of many common publishers. Alternatively, a custom BodyPublisher implementation can be used.

    • Constructor Detail

      • HttpRequest

        protected HttpRequest()
        Creates an HttpRequest.
    • Method Detail

      • newBuilder

        public static HttpRequest.Builder newBuilder​(URI uri)
        Creates an HttpRequest builder with the given URI.
        uri - the request URI
        a new request builder
        IllegalArgumentException - if the URI scheme is not supported.
      • newBuilder

        public static HttpRequest.Builder newBuilder()
        Creates an HttpRequest builder.
        a new request builder
      • bodyPublisher

        public abstract Optional<HttpRequest.BodyPublisher> bodyPublisher()
        Returns an Optional containing the HttpRequest.BodyPublisher set on this request. If no BodyPublisher was set in the requests's builder, then the Optional is empty.
        an Optional containing this request's BodyPublisher
      • method

        public abstract String method()
        Returns the request method for this request. If not set explicitly, the default method for any request is "GET".
        this request's method
      • timeout

        public abstract Optional<Duration> timeout()
        Returns an Optional containing this request's timeout duration. If the timeout duration was not set in the request's builder, then the Optional is empty.
        an Optional containing this request's timeout duration
      • expectContinue

        public abstract boolean expectContinue()
        Returns this request's expect continue setting.
        this request's expect continue setting
      • uri

        public abstract URI uri()
        Returns this request's URI.
        this request's URI
      • version

        public abstract Optional<HttpClient.Version> version()
        Returns an Optional containing the HTTP protocol version that will be requested for this HttpRequest. If the version was not set in the request's builder, then the Optional is empty. In that case, the version requested will be that of the sending HttpClient. The corresponding HttpResponse should be queried to determine the version that was actually used.
        HTTP protocol version
      • headers

        public abstract HttpHeaders headers()
        The (user-accessible) request headers that this request was (or will be) sent with.
        this request's HttpHeaders
      • equals

        public final boolean equals​(Object obj)
        Tests this HTTP request instance for equality with the given object.

        If the given object is not an HttpRequest then this method returns false. Two HTTP requests are equal if their URI, method, and headers fields are all equal.

        This method satisfies the general contract of the Object.equals method.

        equals in class Object
        obj - the object to which this object is to be compared
        true if, and only if, the given object is an HttpRequest that is equal to this HTTP request
        See Also:
        Object.hashCode(), HashMap