Package org.apache.juneau.rest.client
Class RestResponse
java.lang.Object
org.apache.juneau.rest.client.RestResponse
- All Implemented Interfaces:
Closeable,AutoCloseable
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 Summary
Modifier and TypeMethodDescriptionassertOk()Asserts that the status code is in the 2xx range.assertStatus(int expected) Asserts that the status code equals the expected value.Returns a fluent assertion object for this response.body()Returns a fluent body accessor for this response.voidclose()Returns the response body as a UTF-8 string, ornullif there is no body.Returns the raw response body stream, ornullif there is no body.byte[]Returns response body bytes captured for debug logging (up to the configured body cap).longReturns the total response body length observed while reading/draining, or-1if unknown.Returns the client that produced this response.getFirstHeader(String name) Returns the first response header with the given name (case-insensitive), ornullif absent.Returns all response headers.Returns the reason phrase (e.g.Returns theX-Request-Idcorrelation id the server echoed for this exchange.intReturns the HTTP status code.Returns a fluent header accessor for the named response header.
-
Method Details
-
getClient
Returns the client that produced this response.- Returns:
- The client that produced this response. Never
null .
-
getStatusCode
Returns the HTTP status code.- Returns:
- The status code (e.g.
200).
-
getReasonPhrase
Returns the reason phrase (e.g."OK"), ornullif absent.- Returns:
- The reason phrase, possibly
null .
-
getRequestId
Returns theX-Request-Idcorrelation id the server echoed for this exchange.Captured at
onConnectfrom the response header (which may differ from the sent id if the server sanitized it). Returnsnull when the server echoed no id, or on a synthetic no-response error whereonConnectnever 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), ornullif absent.- Parameters:
name- The header name. Must not benull .- Returns:
- The first matching header, or
null if absent.
-
getBodyAsString
Returns the response body as a UTF-8 string, ornullif 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, ornullif there is no body.Callers must not close this stream directly — close the
RestResponseinstead.- 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
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
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
Returns a fluent header accessor for the named response header.- Parameters:
name- The header name (case-insensitive). Must not benull .- Returns:
- A new header accessor. Never
null .
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
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
Returns the total response body length observed while reading/draining, or-1if unknown.- Returns:
- The total body length.
-