Java HttpClient – Set or add header
In this article, we will look at different methods to set headers in a request in Apache HttpClient with examples.
Header is a key-value pair and is used to provide information to client and server or exchange information between them.
If you are not familiar with terms like client, request in HttpClient or do not know how to send request with Apache HttpClient, refer this or this page.
Objects of
HttpRequest
such as HttpGet
or HttpPost
provide a setHeader()
method which is used to set value of an existing header or add a new header to the request.
There are 3 overloaded versions of setHeader()
A. setHeader(Header header)
This method accepts a Header
object. Header
is an interface in HttpClient
, so you can use BasicHeader
, its implementation class. Example,
HttpPost post = new HttpPost("https://application-url"); post.setHeader(new BasicHeader("site-name", "codippa.com"));
B. setHeader(Header[] headers)
This method accepts an array of headers as arguments so that you can create headers at a single place and directly assign them to request at once. Example,
HttpPost post = new HttpPost("https://application-url"); Header[] headers = [ new BasicHeader("site-name", "codippa.com"), new BasicHeader("header-1", "value-1") ]; post.setHeader(headers);
C. setHeader(String, String)
With this method, you can directly add a header and its value, instead of creating an additional object. Example,
HttpPost post = new HttpPost("https://application-url"); post.setHeader("site-name", "codippa");
All setHeader()
methods will overwrite an existing header with same name and add header, if not already exists.
Objects of
HttpRequest
such as HttpGet
or HttpPost
also provide an addHeader()
method which is used to add a header to existing request headers.There are two overloaded versions of
addHeader()
as explained below.A. addHeader(Header header)
This method accepts a
Header
object. Header
is an interface in HttpClient
, so you can use BasicHeader
, its implementation class. Example,
HttpPost post = new HttpPost("https://application-url"); post.addHeader(new BasicHeader("site-name", "codippa.com"));
B. addHeader(String, String)
With this method, you can directly add a header and its value, instead of creating an additional object. Example,
HttpPost post = new HttpPost("https://application-url"); post.addHeader("site-name", "codippa");
setHeader()
also executes the same code as addHeader()
internally.Setting headers on client
A client object in Apache HttpClient is used to send requests, be it a GET, POST, PUT or any other.
Instead of defining headers on request, you can also add default headers on client so that they will be appended to every request. Example,
List<BasicHeader> headers = List.of( new BasicHeader("site-name", "codippa.com")); CloseableHttpClient client = HttpClientBuilder.create(). setDefaultHeaders(headers). build();
Note that with this method, you need to create client using HttpClientBuilder
since setDefault()
method is available in it.
Also, setDefault()
accepts a list of headers as argument.