Skip to main content

NG Responses

run() returns a RestResponse. It is Closeable and holds the connection until closed, so always use try-with-resources.

try (var resp = client.get("/users/42").run()) {
int status = resp.getStatusCode();
String type = resp.header("Content-Type").orElse("unknown");
User user = resp.body().as(User.class);
}

Status​

getStatusCode(), getReasonPhrase(), and getRequestId() (the X-Request-Id that was sent). assertOk() and assertStatus(int) throw RestCallException (with getStatusCode()) when the status does not match.

Headers​

getHeaders() and getFirstHeader(name) return the raw values. header(name) returns a typed ResponseHeader:

AccessorReturns
isPresent(), getName()Presence and name.
getValue(), orElse(String), asOptional()Raw string value.
getValues(), asCsvList()Repeated or comma-separated values.
asInteger(), asLong(), asBoolean()Numeric and boolean parsing.
asZonedDateTime()HTTP-date parsing.
asUri(), asEntityTag(), asEntityTags(), asStringRanges()Structured header types.

See also Typed Response Header Accessors.

Body​

body() returns a ResponseBody. The body can be consumed once.

MethodResult
asString()Decoded text.
asBytes() / readAllBytes()Entire body as a byte array.
asStream()The raw InputStream; you must close it.
as(Class<T>)Parses into a POJO using the parser selected by the response Content-Type. Throws a 415 UnsupportedMediaType if no registered parser matches. See Content-Type Negotiation.
as(Parser, Class<T>)Parses with an explicit parser.
asCursor(...)Lazy record iteration. See Streaming Cursors.
asEventStream()Server-sent events.

getBodyAsString() and getBodyStream() on the response are shortcuts for the common cases.

Fluent assertions​

assertThat() returns a ResponseAssertion for test-style checks; see NG Assertions.