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

Introduction

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.

Topics you will learn from this post

  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?

Prerequisite

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



    io.rest-assured
    rest-assured
    4.3.1
    test

Retrieve headers from the response

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); }

How to assert if a header is present in the response

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

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

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

Leave a Reply

Your email address will not be published. Required fields are marked *