Get response headers from Apache HttpClient
In this article, we will take a look at how to get response headers in Apache HttpClient library.
We will be using a Fake rest api URL, https://jsonplaceholder.typicode.com/posts/1
, which returns a json response.
HttpClient response headers
To read response headers, first we need to execute a GET request using Apache HttpClient and get an actual response.
HttpClient returns a response as an object of CloseableHttpResponse
as a result of calling execute()
method on client object.
To know how to send a GET request with Apache HttpClient, refer this article.
Once we have an object of CloseableHttpResponse
, we can read response headers using its getAllHeaders()
method.
This method returns an array of Header
objects. To get the name and value of header, we need to use its getName()
and getValue()
methods respectively.
HttpClient response headers example
Below is an example of reading response headers using Apache HttpClient library.
// create client CloseableHttpClient client = HttpClients.createDefault(); // create request HttpGet get = new HttpGet( "https://jsonplaceholder.typicode.com/posts"); try { // send GET request CloseableHttpResponse response = client.execute(get); // read headers Header[] allHeaders = response.getAllHeaders(); // iterate over headers for (Header header : allHeaders) { System.out.println("Header: " + header.getName() + ", Value: " + header.getValue()); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
Note that we use an enhanced for loop to iterate over the array of headers.
Below is the output
Header: Date, Value: Sun, 22 May 2022 13:30:16 GMT
Header: Content-Type, Value: application/json; charset=utf-8
Header: Connection, Value: keep-alive
Header: Access-Control-Allow-Credentials, Value: true
Header: Cache-Control, Value: max-age=43200
Header: Pragma, Value: no-cache
Header: Expires, Value: -1
Header: Etag, Value: W/”6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM”
Some headers have been omitted for clarity.
HttpClient specific response headers
If you are interested in some specific header value and do not want to read all headers, then CloseableHttpResponse
provides a getHeaders()
method.
getHeaders()
takes a header name as argument and returns an array of all headers matching that name. Example,
Header[] allHeaders = response.getHeaders("Content-Type"); for (Header header : allHeaders) { System.out.println("Header: " + header.getName() + ", Value: " + header.getValue()); }
This prints
Header: Content-Type, Value: application/json; charset=utf-8
Remember below points when defining header name
1. Name of the header should be an exact match. Header name “Content” will not be matched for “Content-Type”.
2. getHeaders()
performs a case insensitive comparison. So, you can give the header names in any case.
If getHeaders()
could not find a header matching the given name, it will return an empty array instead of a null
value.
Hope the article was useful.