3

For example, you can extract the status code from a CloseableHTTPResponse using response.getStatusLine().getStatusCode().

Is there a similar way to get a specific header value (ie. Location, Date, etc.) from the response (in String format)?

1

1 Answer 1

4

You can do it using getFirstHeader("key").

Apache HttpClient example:

HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet("http://api.com");
HttpResponse response = client.execute(request);

//get header by 'key'
String headerName= response.getFirstHeader("headerName").getValue();

If you want to get all the headers, you can use getAllHeaders().

//get all headers       
    Header[] headers = response.getAllHeaders();
    for (Header header : headers) {
        System.out.println("Key : " + header.getName() 
              + " ,Value : " + header.getValue());
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.