{title:'Response Headers', created:'8.2.0', updated:'9.0.0'}

Response headers are accessed through the following methods:

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

Example:

| RestResponse res = client.get(URI).complete(); | ResponseHeader header = res.getHeader("Location"); | | // See if response contains Location header. | boolean hasLocationHeader = header.isPresent(); | | // Get actual value if it exists. | String locationHeader1 = header.orElse("http://localhost"); | | // Converted to object. | URI locationHeader2 = header.as(URI.class).orElse(null);

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

The {@link oajrc.ResponseHeader#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 oajrc.RestResponse} object after the assertion has been completed:

Example:

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