Skip to main content

Request Parts

Per-client or per-request HTTP parts (headers, query/form data, path parameters) can be manipulated via the following methods that return back builders for those parts:

RestClient.Builderheaders() formData() queryData() pathData()RestRequestgetHeaders() getFormData() getQueryData() getPathData()

Convenience methods are also provided for quickly adding parts:

RestClient.Builderheader(String,String) header(String,Supplier) headers(Header...) headersDefault(Header...)formData(NameValuePair...) formData(String,String) formData(String,Supplier) formDataDefault(NameValuePair...)queryData(NameValuePair...) queryData(String,String) queryData(String,Supplier) queryDataDefault(NameValuePair...)pathData(NameValuePair...) pathData(String,String) pathData(String,Supplier) pathDataDefault(NameValuePair...)RestRequestheader(Header) header(String,Object) headerPairs(String...) headers(Header...) headersBean(Object)formData(NameValuePair...) formData(String,Object) formDataBean(Object) formDataCustom(Object) formDataPairs(String...)queryData(NameValuePair...) queryData(String,Object) queryDataBean(Object) queryDataPairs(String...)pathData(NameValuePair...) pathData(String,Object) pathDataBean(Object) pathDataPairs(String...)
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().getContent().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();
See Also

Constant Parts on Remote-Proxy Interfaces​

The above methods add parts on a per-client or per-request basis. When using the next-generation remote-proxy engine (RestClient.remote(...)), you can also declare always-applied constant headers, query parameters, and form-data fields directly on the proxy annotations — with no corresponding method parameter:

  • Interface-level: @Remote(headers=…, queryData=…, formData=…) — applied to every method.
  • Method-level: @RemoteOp(headers=…, queryData=…, formData=…) (and the verb annotations) — applied to that method, taking precedence over an interface-level constant of the same name.
Example
@Remote(path="/api", queryData="api_key=$S{petstore.apiKey}")
public interface PetStore {

@RemoteGet(path="/pets", headers="X-View: full", queryData="view=full")
Pet[] getPets();
}

These constants are honored by the next-gen engine only; the classic getRemote(...) engine ignores them. See NG Remote Proxies for the full contract (precedence, VarResolver support, and composition with caller-supplied values).