Class ResponseBody
RestResponse.
Provides convenient methods for reading the body as a string, byte array, or stream. The body is read lazily; each method reads and returns the body content once.
Obtain instances via RestResponse.body().
See Also:
- Since:
- 9.2.1
-
Method Summary
Modifier and TypeMethodDescription<T> TParses the response body totypeusing the parser negotiated from the responseContent-Type.<T> TParses the response body totypeusing the given parser.byte[]asBytes()Returns the response body as a byte array.<T> TOpens a token/record-streaming cursor over the response body using the parser negotiated from the responseContent-Type.<T> TOpens a token/record-streaming cursor over the response body using the given parser.Opens a closeable, cancellable cursor over atext/event-stream response body.asStream()Returns the raw response body stream.asString()Returns the response body as a UTF-8 string.Returns the response body decoded with the given charset.byte[]Reads the response body into a byte array and returns it, or returnsnullif there is no body.
-
Method Details
-
asString
Returns the response body as a UTF-8 string.- Returns:
- The body string, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body.
-
asString
Returns the response body decoded with the given charset.- Parameters:
charset- The character set to use for decoding. Must not benull .- Returns:
- The body string, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body.
-
asBytes
Returns the response body as a byte array.- Returns:
- The body bytes, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body.
-
asStream
Returns the raw response body stream.Callers should not close this stream directly; close the parent
RestResponseinstead.- Returns:
- The body stream, or
null if the response has no body.
-
readAllBytes
Reads the response body into a byte array and returns it, or returnsnullif there is no body.Equivalent to
asBytes().- Returns:
- The body bytes, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body.
-
asCursor
Opens a token/record-streaming cursor over the response body using the parser negotiated from the responseContent-Type.Equivalent to
asCursor(negotiatedParser, type)where the parser is selected from the responseContent-Typeheader; when no registered parser matches and no default parser is configured, a415 Unsupported Media Type is thrown. UseasCursor(Parser, Class)to read with an explicit parser.- Type Parameters:
T- The cursor type (RecordReader,TokenReader, or a concrete subtype).- Parameters:
type- The declared cursor type. Must not benull .- Returns:
- A cursor over the live response body. Never
null . - Throws:
IOException- If the body is missing, the parser does not support the requested cursor surface, or the produced cursor is not assignable totype.
-
as
Parses the response body totypeusing the parser negotiated from the responseContent-Type.When the header is absent or matches no registered parser and no default parser is configured, a
415 Unsupported Media Type is thrown. Useas(Parser, Class)to force a specific parser, bypassing content negotiation.- Type Parameters:
T- The type to parse to.- Parameters:
type- The type to parse to. Must not benull .- Returns:
- The parsed body, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body or the body could not be parsed.
-
as
Parses the response body totypeusing the given parser.This forces the supplied parser, bypassing the
Content-Typenegotiation performed byas(Class).A parse failure is surfaced strictly as an
IOExceptionwrapping the underlyingParseException; the malformed body is never returned to the caller.- Type Parameters:
T- The type to parse to.- Parameters:
parser- The parser to use. Must not benull .type- The type to parse to. Must not benull .- Returns:
- The parsed body, or
null if the response has no body. - Throws:
IOException- If an I/O error occurs reading the body or the body could not be parsed.
-
asCursor
Opens a token/record-streaming cursor over the response body using the given parser.This forces the supplied parser, bypassing the
Content-Typenegotiation performed byasCursor(Class).The cursor reads directly from the live response stream — the body is not buffered into memory. The caller owns the returned cursor and the parent
RestResponse; close them when done.When
typeis (or extends)TokenReaderthe parser must implementTokenReadable; otherwise it must implementRecordReadable.- Type Parameters:
T- The cursor type (RecordReader,TokenReader, or a concrete subtype).- Parameters:
parser- The parser that opens the cursor. Must not benull .type- The declared cursor type. Must not benull .- Returns:
- A cursor over the live response body. Never
null . - Throws:
IOException- If the body is missing, the parser does not support the requested cursor surface, or the produced cursor is not assignable totype.
-
asEventStream
Opens a closeable, cancellable cursor over atext/event-stream response body.Reads the live response body as UTF-8 and dispatches one
SseEventat a time via the returnedSseEventReader. The stream is not buffered into memory — events are parsed incrementally as bytes arrive, which is required for a long-lived Streamable-HTTP channel (seejuneau-rest-client-mcp).Closing the returned reader first closes the parent
RestResponse(transport-layer cancel / release), then closes the reader itself. This ordering is what makes the cursor cancellable: callers can stop consuming an open SSE channel at any time by callingSseEventReader.close(), including from another thread, without waiting for the server to finish sending events.Transport-specific release behavior varies: this API guarantees that both the response and reader are closed in the above order, but the exact connection-release mechanism is determined by the active transport.
Notes:
- This cursor is not thread-safe, except for using
SseEventReader.close()as a cancellation signal from another thread. - Mid-stream read failures are surfaced as
UncheckedIOExceptionfromSseEventReader.hasNext()/SseEventReader.next()(perSseEventReadercontract). - Iterating after close throws.
- Returns:
- A cursor over the live response body. Never
null . - Throws:
IOException- If the response has no body to open a stream over.
- This cursor is not thread-safe, except for using
-