{8.2.0-new} Request Headers

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

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.