REST Assured Tutorial 48 – How To Pass Headers In Rest Assured Requests

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn to pass or add headers information to a Rest Assured requests.

Prerequisite

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



    io.rest-assured
    rest-assured
    4.3.1
    test

What are headers?

Headers are metadata(data about data) of request and response of an API. For example- We pass request payload in a format. It may be JSON, XML, or any other type. Similarly, the response may be in any format. We can use headers with requests and responses which will help the server and client to understand requests and responses well.

We can also use headers to pass authorization information. When we hit an API, many default headers are added with requests and responses to establish a better and secure connection. Some mostly encountered headers are “Content-Type”, “Accept”, “Authorization”, “ETag” etc.

Headers can be a key-value pair or key with multi-value. We will see how can we pass headers to a request in Rest Assured scripts.

How to add headers to Rest Assured request?

Interfaces RequestSpecification and ResponseSpecification have multiple overloaded methods named “headers()” and “header()”. We can use headers() or header() methods of RequestSpecification to send headers with a request to an API and use headers() and header() methods of ResponseSpecification to put assertions on headers received with a response. That is a tiny difference between these two. Don’t think that you are going to add headers for a response using headers() or header() methods of ResponseSpecification.

Overloaded forms of headers() and header() methods

1. RequestSpecification headers(String firstHeaderName, Object firstHeaderValue, Object... headerNameValuePairs);
2. RequestSpecification headers(Map headers);
3. RequestSpecification headers(Headers headers);
4. RequestSpecification header(String headerName, Object headerValue, Object... additionalHeaderValues);
5. RequestSpecification header(Header header);

Do not confuse with these many overloaded methods. All are just to make your job easier.

Adding a header in multiple ways

Suppose a header name is “someHeader” and its value is “somevalue“, then we can pass that header to request in multiple ways as shown below:-

// Add a header as key value
RestAssured.given().header("someHeader","somevalue");
RestAssured.given().headers("someHeader","somevalue");
		
// Add header as a Map
Map requestHeaders = new HashMap<>();
requestHeaders.put("someHeader","somevalue");
RestAssured.given().headers(requestHeaders);
		
// Add header using Header class
Header requestHeader1 = new Header("someHeader","somevalue");
RestAssured.given().header(requestHeader1);
		
// Add header using Header and Headers class
Header requestHeader2 = new Header("someHeader","somevalue");
Headers requestHeaders3 = new Headers(requestHeader2);
RestAssured.given().headers(requestHeaders3);

You can observe that I used all overloaded forms of headers() and header() methods. They just accept input in different formats. header() is used to add single header while headers() is used to add multiple headers.

Header is a class that represents a single header while Headers is also a class that represents a collection of headers. You need to use Header and Headers constructors to create header information.

Adding duplicate headers

You can pass duplicate headers as well and there will not be any overwritten of values. For example, If we pass two values of header1 as value1 and value2 then it will be merged and will be passed as header1=value1 and header1=value2. It is the default behaviour. Even header values are same then also duplicate headers will be sent.

You can call the same method multiple times or mix methods. All headers will be added to the request irrespective of unique or duplicate headers. If you have a header with multi values then you can use a dedicated method given below:-

RequestSpecification header(String headerName, Object headerValue, Object… additionalHeaderValues);

Example:-

RestAssured.given().header("someHeader","someFirstvalue", "someSecondvalue");

Be careful if you are storing header values in a Map as it will not have duplicate keys. Do not use it if you need to pass a header with multiple values.

How to change default merging behaviour of headers?

By default header value will not be overwritten if used multiple times. They are merged by default except “Content-Type” and “Accept”. You can override this behaviour for the individual headers by using HeaderConfig class.

// Define headers that should be be merged instead of overwritten 
mergeHeadersWithName(headerName, additionalHeaderNames)
// Define headers that should be overwritten instead of merged
overwriteHeadersWithName(headerName, additionalHeaderNames)

Example-

RestAssuredConfig.config().headerConfig(HeaderConfig.headerConfig().overwriteHeadersWithName(“header1”));

If we pass two values of header1 as value1 and value2 then it will not be merged and last value will be final i.e. only one value of header1 will be passed as header1=value1.

RestAssuredConfig.config().headerConfig(HeaderConfig.headerConfig().mergeHeadersWithName(“header1”));

If we pass two values of header1 as value1 and value2 then it will be merged and will be passed as header1=value1 and header1=value2. It is default behaviour but needs to use if you have changed behaviour and want to restore default behaviour.

You can use any way to add headers to a request. It depends upon your requirements and coding standards.

Example Code

package RestAssuredBasicConcepts;

import java.util.HashMap;
import java.util.Map;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.config.HeaderConfig;
import io.restassured.config.RestAssuredConfig;
import io.restassured.http.Header;
import io.restassured.http.Headers;

public class AddingHeaders {
	
	@Test
	public void addKeyvaluePairHeaders() {
		
		// Add a header as key value
		RestAssured.given().header("someHeader","somevalue");
		RestAssured.given().headers("someHeader","somevalue");
		
		// Add header as a Map
		Map requestHeaders = new HashMap<>();
		requestHeaders.put("someHeader","somevalue");
		RestAssured.given().headers(requestHeaders);
		
		// Add header using Header class
		Header requestHeader1 = new Header("someHeader","somevalue");
		RestAssured.given().header(requestHeader1);
		
		// Add header using Header and Headers class
		Header requestHeader2 = new Header("someHeader","somevalue");
		Headers requestHeaders3 = new Headers(requestHeader2);
		RestAssured.given().headers(requestHeaders3);
		
		// Add header with multiple values
		RestAssured.given().header("someHeader","someFirstvalue", "someSecondvalue");
		
		// Changing default behavior of merging
		RestAssuredConfig.config().headerConfig(HeaderConfig.headerConfig().overwriteHeadersWithName("header1"));
		
		// Change overwrite behavior if not
		RestAssuredConfig.config().headerConfig(HeaderConfig.headerConfig().mergeHeadersWithName("header1"));	
		
	}
}

You can use log() method to view added headers as below:-

RestAssured
   .given()
   .headers("someHeader","somevalue")
   .log()
   .headers()
   .get("https://restful-booker.herokuapp.com/booking/10");

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.

Leave a Reply

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