Class RestResponse

java.lang.Object
org.apache.juneau.rest.client.RestResponse
All Implemented Interfaces:
Closeable, AutoCloseable

public final class RestResponse extends Object implements Closeable
An HTTP response returned by RestRequest.run().

Wraps the transport-layer TransportResponse and provides higher-level accessors. Callers must close this response (it implements Closeable) to release transport resources.

Since:
9.2.1
  • Method Details

    • getClient

      Returns the client that produced this response.
      Returns:
      The client that produced this response. Never null.
    • getStatusCode

      public int getStatusCode()
      Returns the HTTP status code.
      Returns:
      The status code (e.g. 200).
    • getReasonPhrase

      Returns the reason phrase (e.g. "OK"), or null if absent.
      Returns:
      The reason phrase, possibly null.
    • getRequestId

      public String getRequestId()
      Returns the X-Request-Id correlation id the server echoed for this exchange.

      Captured at onConnect from the response header (which may differ from the sent id if the server sanitized it). Returns null when the server echoed no id, or on a synthetic no-response error where onConnect never ran — it never silently falls back to the sent id.

      Returns:
      The echoed correlation id, or null if none was observed.
      Since:
      10.0.0
    • getHeaders

      Returns all response headers.
      Returns:
      An unmodifiable list. Never null.
    • getFirstHeader

      Returns the first response header with the given name (case-insensitive), or null if absent.
      Parameters:
      name - The header name. Must not be null.
      Returns:
      The first matching header, or null if absent.
    • getBodyAsString

      Returns the response body as a UTF-8 string, or null if there is no body.
      Returns:
      The body as a string, possibly null.
      Throws:
      IOException - If an I/O error occurs reading the body.
    • getBodyStream

      Returns the raw response body stream, or null if there is no body.

      Callers must not close this stream directly — close the RestResponse instead.

      Returns:
      The body stream, possibly null.
    • assertOk

      Asserts that the status code is in the 2xx range.
      Returns:
      This object (for chaining).
      Throws:
      RestCallException - If the status code is not 2xx.
    • assertStatus

      public RestResponse assertStatus(int expected) throws RestCallException
      Asserts that the status code equals the expected value.
      Parameters:
      expected - The expected status code.
      Returns:
      This object (for chaining).
      Throws:
      RestCallException - If the status code does not match.
    • body

      public ResponseBody body()
      Returns a fluent body accessor for this response.
      Returns:
      A new body accessor. Never null.
    • assertThat

      Returns a fluent assertion object for this response.

      Use this for test-style validation of the response in production or test code:

      resp.assertThat() .statusCode(200) .body().contains("alice");

      Returns:
      A new assertion object. Never null.
    • header

      public ResponseHeader header(String name)
      Returns a fluent header accessor for the named response header.
      Parameters:
      name - The header name (case-insensitive). Must not be null.
      Returns:
      A new header accessor. Never null.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getCachedContent

      public byte[] getCachedContent()
      Returns response body bytes captured for debug logging (up to the configured body cap).
      Returns:
      Captured response bytes, or null if not captured.
    • getCachedContentLength

      public long getCachedContentLength()
      Returns the total response body length observed while reading/draining, or -1 if unknown.
      Returns:
      The total body length.