Class HttpResponse<T>

  • Type Parameters:
    T - the response body type

    public abstract class HttpResponse<T>
    extends Object
    Represents a response to a HttpRequest.
    Incubating Feature. Will be removed in a future release.

    A HttpResponse is available when the response status code and headers have been received, and typically after the response body has also been received. This depends on the response body handler provided when sending the request. In all cases, the response body handler is invoked before the body is read. This gives applications an opportunity to decide how to handle the body.

    Methods are provided in this class for accessing the response headers, and response body.

    Response handlers and subscribers

    Response bodies are handled at two levels. Application code supplies a response handler (HttpResponse.BodyHandler) which may examine the response status code and headers, and which then returns a HttpResponse.BodySubscriber to actually read (or discard) the body and convert it into some useful Java object type. The handler can return one of the pre-defined subscriber types, or a custom subscriber, or if the body is to be discarded it can call discard and return a subscriber which discards the response body. Static implementations of both handlers and subscribers are provided in BodyHandler and BodySubscriber respectively. In all cases, the handler functions provided are convenience implementations which ignore the supplied status code and headers and return the relevant pre-defined BodySubscriber.

    See HttpResponse.BodyHandler for example usage.

    Since:
    9
    • Constructor Detail

      • HttpResponse

        protected HttpResponse()
        Creates an HttpResponse.
    • Method Detail

      • statusCode

        public abstract int statusCode()
        Returns the status code for this response.
        Returns:
        the response code
      • request

        public abstract HttpRequest request()
        Returns the HttpRequest corresponding to this response.

        This may not be the original request provided by the caller, for example, if that request was redirected.

        Returns:
        the request
        See Also:
        previousResponse()
      • previousResponse

        public abstract Optional<HttpResponse<T>> previousResponse()
        Returns an Optional containing the previous intermediate response if one was received. An intermediate response is one that is received as a result of redirection or authentication. If no previous response was received then an empty Optional is returned.
        Returns:
        an Optional containing the HttpResponse, if any.
      • headers

        public abstract HttpHeaders headers()
        Returns the received response headers.
        Returns:
        the response headers
      • body

        public abstract T body()
        Returns the body. Depending on the type of T, the returned body may represent the body after it was read (such as byte[], or String, or Path) or it may represent an object with which the body is read, such as an InputStream.

        If this HttpResponse was returned from an invocation of previousResponse() then this method returns null

        Returns:
        the body
      • sslParameters

        public abstract SSLParameters sslParameters()
        Returns the SSLParameters in effect for this response. Returns null if this is not a HTTPS response.
        Returns:
        the SSLParameters associated with the response
      • uri

        public abstract URI uri()
        Returns the URI that the response was received from. This may be different from the request URI if redirection occurred.
        Returns:
        the URI of the response
      • version

        public abstract HttpClient.Version version()
        Returns the HTTP protocol version that was used for this response.
        Returns:
        HTTP protocol version