Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 49 – How To Retrieve Single and MultiValue Headers From Response Using Rest Assured

Posted on 03/21/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, we will learn to retrieve header values from response.

When we send a request to a server using an API, the server sends some headers along with the response. We may need to assert those response headers as part of testing. To assert header values we need to get headers from the response which we will learn in this post.

Note here that I am talking about Response headers not request headers. You have control over request headers except for some auto-generated headers. You can easily retrieve headers added by us to a request.

  1. Retrieve single value headers from the response
  2. Retrieve multivalue headers from the response
  3. Usage of getHeaders(), getHeader(String headerName), headers(), header(String headerName)
  4. How to assert if a header is present in the response headers?

I have used below dependency of Rest Assured library for this post:-

  io.rest-assured rest-assured 4.3.1 test

Once you hit the API request you need to extract the response as Response. Note here that Response is an interface that represents the response of a request made by REST Assured. An example is below:-

Response response= 
        RestAssured
            .given()
            .get("https://restful-booker.herokuapp.com/booking/1");

OR

Response response= 
        RestAssured
            .given()
            .get("https://restful-booker.herokuapp.com/booking/1")
            .then()
            .extract()
            .response();

The response interface provides four methods(effectively two) to retrieve a header and headers.

header(String headerName) or getHeader(String headername) – Get a single header value associated with the given name.

headers() or getHeaders() – Get all response headers

REST Assured contains some methods that are only there for syntactic sugar. The two methods above are examples of that. getHeader() sounds better than header().

Since we know that a header is a key-value or key-multivalue pairs. So if you want to retrieve a specific header by its name then use getHeader(String headerName) or header(String headerName) who returns a String which is actually the value of that header. If you want to retrieve all headers of response then use headers() or getHeaders() who return Headers object.

package RestAssuredBasicConcepts;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.Header;
import io.restassured.http.Headers;
import io.restassured.response.Response;

public class RetrievingResponseHeaders {

        @Test
        public void getALlHeadersFromResponse()
        {
                Response response= 
                                RestAssured
                                        .given()
                                        .get("https://restful-booker.herokuapp.com/booking/1")
                                        .then()
                                        .extract()
                                        .response();
                
                System.out.println("All Headers of response are :- ");
                Headers allHeaders = response.getHeaders();
                for(Header header : allHeaders)
                {
                        System.out.print(header.getName() +" : ");
                        System.out.println(header.getValue());
                }
                
                System.out.println("Value of Header Content-Type : "+response.getHeader("Content-Type"));
                
        }       
        
}

As we know that a header may be a multivalue header as well in that case you need to twist a bit to retrieve all values or a list of values of a given header as shown below:-

// Suppose Content-Type is a multivalue header
List

allValue = response.getHeaders().getList(“Content-Type”); for(Header header : allValue) { System.out.print(header.getName() +” : “); System.out.println(header.getValue()); } List allValue1 = response.getHeaders().getValues(“Content-Type”); for(String value : allValue1) { System.out.println(value); }

If we try to retrieve a non-existing header from the response it returns NULL. We can have a user-defined method that can be used to find if a header is present in response based on this behaviour.

But there is a direct method already available named hasHeaderWithName(String headerName). It returns true if the header is present otherwise false.

boolean isPresent = response.getHeaders().hasHeaderWithName("Content-Type");

If you use getHeaders() then also you can retrieve header values using get() or getValue() as shown below:-

String v1 = response.getHeaders().get("Content-Type").getValue();
String v2 = response.getHeaders().getValue("Content-Type");

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Advanced TestNG Tutorials 34: How To Pass a Group Name to be Run at Runtime in TestNG XML Using Beanshell
Next Post: Tools To Find XPath In Chrome Browser

Related Posts

API Testing – Postman Uncategorized
Handling Website Popups In Selenium webdriver | Make Selenium Easy Uncategorized
April 18, 2017 – Make Selenium Easy Uncategorized
components – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 28- Building Workflow in Postman Using Collection Runner – New Feature of Postman Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark