Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/2025 By admin

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.

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

  io.rest-assured rest-assured 4.3.1 test

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.

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.

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.

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.

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.

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

Uncategorized

Post navigation

Previous Post: API Testing Tutorial Part 8 – Introduction to REST & REST API
Next Post: Selenium Interview Question 2 – Can Selenium Be Used For API Automation?

Related Posts

TestNG Tutorials 40: Groups of Groups or MetaGroups in TestNG Uncategorized
REST Assured Tutorial 18 – Querying RequestSpecification in Rest Assured Uncategorized
September 26, 2017 – Make Selenium Easy Uncategorized
promptPopup – Make Selenium Easy Uncategorized
TestNG Tutorials 48: How to Pass Parameters of Different Datatypes in TestNG Uncategorized
JavascriptSetDate – Make Selenium Easy 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