{8.2.0-new} Response Headers

Response headers are accessed through the following methods:

Unlike {@link oajr.client2.RestResponse#getFirstHeader(String)} and {@link oajr.client2.RestResponse#getLastHeader(String)}, the {@link oajr.client2.RestResponse#getHeader(String)} method returns an empty {@link oajr.client2.RestResponseHeader} object instead of returning null. This allows it to be used more easily in fluent calls.

Example:

// See if response contains Location header. boolean hasLocationHeader = client.get(URI).complete().getHeader("Location").exists();

The {@link oajr.client2.RestResponseHeader} class extends from the HttpClient {@link org.apache.http.Header} class and provides several convenience methods:

The {@link oajr.client2.RestResponseHeader#schema(HttpPartSchema)} method allows you to perform parsing of OpenAPI formats for header parts.

Example:

// Parse the header "Foo: bar|baz". List<String> fooHeader = client .get(URI) .complete() .getHeader("Foo").schema(T_ARRAY_PIPES).as(List.class, String.class);

Assertion methods are also provided for fluent-style calls:

Note how in the following example, the fluent assertion returns control to the {@link oajr.client2.RestResponse} object after the assertion has been completed:

Example:

// Assert the response content type is any sort of JSON. String body = client.get(URI) .run() .getHeader("Content-Type").assertString().matchesSimple("application/json*") .getBody().asString();