{8.2.0-new} Request Headers

Per-client or per-request headers can be specified using the following methods:

- {@link oajr.client2.RestClientBuilder}
  - {@link oajr.client2.RestClientBuilder#header(String,Object) header(String,Object)}
  - {@link oajr.client2.RestClientBuilder#header(String,Object,HttpPartSchema) header(String,Object,HttpPartSchema)}
  - {@link oajr.client2.RestClientBuilder#header(String,Supplier) header(String,Supplier<?>)}
  - {@link oajr.client2.RestClientBuilder#header(String,Supplier,HttpPartSchema) header(String,Supplier<?>,HttpPartSchema)}
  - {@link oajr.client2.RestClientBuilder#header(Header) header(Header)}
  - {@link oajr.client2.RestClientBuilder#headers(Object...) headers(Object...)}
  - {@link oajr.client2.RestClientBuilder#headerPairs(Object...) headerPairs(Object...)}
- {@link oajr.client2.RestRequest}
  - {@link oajr.client2.RestRequest#header(String,Object) header(String,Object)}
  - {@link oajr.client2.RestRequest#header(String,Object,HttpPartSchema) header(String,Object,HttpPartSchema)}
  - {@link oajr.client2.RestRequest#header(AddFlag,String,Object) header(AddFlag,String,Object)}
  - {@link oajr.client2.RestRequest#header(AddFlag,String,Object,HttpPartSchema) header(AddFlag,String,Object,HttpPartSchema)}
  - {@link oajr.client2.RestRequest#header(Header) header(Header)}
  - {@link oajr.client2.RestRequest#headers(Object...) headers(Object...)}
  - {@link oajr.client2.RestRequest#headers(AddFlag,Object...) headers(AddFlag,Object...)}
  - {@link oajr.client2.RestRequest#headerPairs(Object...) headers(Object...)}

##### Example:

// Create a client that adds a "Foo: bar" header to every request. RestClient client = RestClient.create().header("Foo","bar").build(); // Or do it on every request. String response = client.get(URI).header("Foo","bar").run().getBody().asString();

{@link oajr.client2.RestClientBuilder#headers(Object...) headers(Object...)} allows you to pass in a variety of header objects, and {@link oajr.client2.RestClientBuilder#headerPairs(Object...) headerPairs(Object...)} allows you to specify several headers in a compact fashion.

##### Example:

// Create a client that adds a bunch of headers to every request. RestClient client = RestClient .create() .headers( AMap.of("Foo","bar","Baz","qux") // Arbitrary key/value pairs. BasicStringHeader.of("Foo","bar") // A Header object. BasicStringHeader.of("Foo",()->getBar()) // A dynamic Header object. Accept.of("application/json") // Predefined Header objects. HeaderSupplier.ofPairs("Foo","bar") // A dynamically changing list of Header objects. AList.of(ContentType.of("application/json")) // A list of anything else on this list. ) .headerPairs("Foo","bar","Baz","qux") .build();

Additionally, methods are provided on the client builder and per request for all standard HTTP headers such as {@link oajr.client2.RestClientBuilder#authorization(Object) authorization(Object)}.

##### Example:

// Create a client that adds an Authorization header to every request. RestClient client = RestClient.create().authorization("Foo").build(); // Or do it per-request. String response = client.get(URI).authorization("Foo").run().getBody().asString(); // Or use an HttpHeader. response = client.get(URI).headers(Authorization.of("Foo")).run().getBody().asString();

The supplier methods are particularly useful for header values whose values may change over time (such as Authorization headers which may need to change every few minutes).

##### Example:

// Create a client that adds a dynamic Authorization header to every request. RestClient client = RestClient.create().header("Authorization", ()->getMyAuthToken()).build();

The {@link oaj.httppart.HttpPartSchema} API allows you to define OpenAPI schemas to POJO data structures on request headers.

##### Example:

// Create a client that adds a header "Foo: bar|baz" to every request. RestClient client = RestClient .create() .header("Foo", AList.of("bar","baz"), T_ARRAY_PIPES) .build();

The methods with {@link oaj.AddFlag} parameters allow you to control whether new headers get appended, prepended, or replace existing headers with the same name.

- Methods that pass in POJOs convert values to strings using the part serializers. Methods that pass in Header or NameValuePair objects use the values returned by that bean directly.

- {@link oaj.http.header} - Predefined {@link org.apache.http.Header} beans.