{8.2.0-new} Response Headers

Response headers are accessed through the following methods:

- {@link oajr.client2.RestResponse}
  - {@link oajr.client2.RestResponse#getHeader(String) getHeader(String)} returns {@link oajr.client2.RestResponseHeader}
  - {@link oajr.client2.RestResponse#getHeaders(String) getHeaders(String)} returns {@link oajr.client2.RestResponseHeader}[]
  - {@link oajr.client2.RestResponse#getFirstHeader(String) getFirstHeader(String)} returns {@link oajr.client2.RestResponseHeader}
  - {@link oajr.client2.RestResponse#getLastHeader(String) getLastHeader(String)} returns {@link oajr.client2.RestResponseHeader}
  - {@link oajr.client2.RestResponse#getAllHeaders() getAllHeaders()} returns {@link oajr.client2.RestResponseHeader}[]
  - {@link oajr.client2.RestResponse#getStringHeader(String) getStringHeader(String)} returns String
  - {@link oajr.client2.RestResponse#containsHeader(String) containsHeader(String)} returns boolean

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:

- {@link oajr.client2.RestResponseHeader}
  - {@link oajr.client2.RestResponseHeader#exists() exists()} returns boolean
  - {@link oajr.client2.RestResponseHeader#asString() asString()} returns String
  - {@link oajr.client2.RestResponseHeader#asOptionalString() asOptionalString()} returns Optional<String>
  - {@link oajr.client2.RestResponseHeader#asStringOrElse(String) asStringOrElse(String)} returns String
  - {@link oajr.client2.RestResponseHeader#as(Type,Type...) as(Type,Type...)} returns T
  - {@link oajr.client2.RestResponseHeader#as(Class) as(Class<T>)} returns T
  - {@link oajr.client2.RestResponseHeader#asOptional(Type,Type...) asOptional(Type,Type...)} returns Optional<T>
  - {@link oajr.client2.RestResponseHeader#asOptional(Class) asOptional(Class<T>)} returns Optional<T>
  - {@link oajr.client2.RestResponseHeader#asMatcher(Pattern) asMatcher(Pattern)} returns {@link java.util.regex.Matcher}
  - {@link oajr.client2.RestResponseHeader#asMatcher(String) asMatcher(String)} returns {@link java.util.regex.Matcher}
  - {@link oajr.client2.RestResponseHeader#asHeader(Class) asHeader(Class<T extends BasicHeader> c)} returns {@link oaj.http.BasicHeader}
  - {@link oajr.client2.RestResponseHeader#asStringHeader() asStringHeader()} returns {@link oaj.http.header.BasicIntegerHeader}
  - {@link oajr.client2.RestResponseHeader#asIntegerHeader() asIntegerHeader()} returns {@link oaj.http.header.BasicStringHeader}
  - {@link oajr.client2.RestResponseHeader#asLongHeader() asLongHeader()} returns {@link oaj.http.header.BasicLongHeader}
  - {@link oajr.client2.RestResponseHeader#asDateHeader() asDateHeader()} returns {@link oaj.http.header.BasicDateHeader}
  - {@link oajr.client2.RestResponseHeader#asCsvArrayHeader() asCsvArrayHeader()} returns {@link oaj.http.header.BasicCsvArrayHeader}
  - {@link oajr.client2.RestResponseHeader#asEntityTagArrayHeader() asEntityTagArrayHeader()} returns {@link oaj.http.header.BasicEntityTagArrayHeader}
  - {@link oajr.client2.RestResponseHeader#asStringRangeArrayHeader() asStringRangeArrayHeader()} returns {@link oaj.http.header.BasicStringRangeArrayHeader}
  - {@link oajr.client2.RestResponseHeader#asUriHeader() asUriHeader()} returns {@link oaj.http.header.BasicUriHeader}

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:

- {@link oajr.client2.RestResponseHeader}
  - {@link oajr.client2.RestResponseHeader#assertString() assertString()} returns {@link oaj.assertions.FluentStringAssertion}
  - {@link oajr.client2.RestResponseHeader#assertInteger() assertInteger()} returns {@link oaj.assertions.FluentIntegerAssertion}
  - {@link oajr.client2.RestResponseHeader#assertLong() assertLong()} returns {@link oaj.assertions.FluentLongAssertion}
  - {@link oajr.client2.RestResponseHeader#assertDate() assertDate()} returns {@link oaj.assertions.FluentDateAssertion}

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();