Table of contents
1.
Introduction
2.
What are Headers?
3.
What is an API Header?
4.
Examples of Headers in REST APIs
5.
How to Pass Headers in Rest Assured Requests?   
5.1.
Passing Them in a Key-Value Format Using The Header Method.
5.2.
Passing Them as a Map Using The Headers Method.
5.3.
Passing Them as a List Using The Headers Method.
6.
How to Retrieve Headers In Rest Assured Requests?   
7.
Frequently Asked Questions
7.1.
What are headers?
7.2.
How to change the default merging behavior of headers?
7.3.
Can we pass duplicate headers?
7.4.
How to pass the basic authorization header in Rest Assured?
7.5.
How do I blacklist a header in Rest Assured?
8.
Conclusion
Last Updated: Mar 5, 2025
Medium

How To Pass or retrieve Headers In Rest Assured Requests

Author Tisha
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When testing RESTful APIs, handling headers in requests is crucial for authentication, content negotiation, and passing metadata. Rest Assured, a popular Java library for API testing, provides an easy way to send and retrieve headers in HTTP requests. Whether you're working with authorization tokens, content types, or custom headers, understanding how to manipulate them effectively ensures smooth communication with APIs. In this blog, we'll explore how to pass and retrieve headers using Rest Assured.

Introduction

What are Headers?

Headers are the data about data of request and the response of an API. It is like as We pass the request payload in a format. It can be XML, JSON or any other type. The response can be in any format. We can use the headers with requests and responses that will help the client and the server to understand responses and requests well.

The headers can also be used to pass the authorization information. When the API is hit, many headers are added with responses and requests to make a better and secure connection. Some encountered headers are “ETag”, “Content-Type”, “Authorization”, “Accept” etc.

Headers can have key with multi-value or key-value pair. Below we will learn how to pass headers in rest assured requests.

What is an API Header?

An API header is a key-value pair sent along with the API request or response to provide additional information about the request or response. Headers are used to convey metadata such as authentication details, content type, caching policies, and more. They play a critical role in API communication by defining how the client and server should handle the request or response.

For example, headers can specify whether the request body is in JSON or XML format or include authorization tokens for secure access.

Examples of Headers in REST APIs

Here are some common headers used in REST APIs:

HeaderDescriptionExample
Content-TypeSpecifies the format of request bodyapplication/json
AuthorizationProvides access tokens for secure APIsBearer ey123xyz
AcceptDefines the response format the client expectsapplication/json
User-AgentIdentifies client informationPostmanRuntime/7.29.0
Cache-ControlSpecifies caching policiesno-cache
CookieSends stored cookiessessionId=abcd1234

How to Pass Headers in Rest Assured Requests?   

There are more than one way with which we can pass headers in Rest Assured Requests. Below are the ways:

Passing Them in a Key-Value Format Using The Header Method.

We can pass headers in Rest Assured requests is by passing them in a key value format using the header method. Below is an example of this.

Example:

Response r = given()
.baseUri("https://www.codingninjas.com/")
.header("header1", "value1")
.header("header2", "value2")
.get("/about/our-team.htm");

Passing Them as a Map Using The Headers Method.

We can also pass headers in rest assured requests is by passing them as a map using the headers method. Below is the example of this.

Example:

Map<String,Object> m = new HashMap<String,Object>();
m.put("header1", "value1");
m.put("header2, "value2");
Response r = given()
.baseUri("https://www.codingninjas.com/")
.headers(m)
.get("/about/our-team.htm");

Passing Them as a List Using The Headers Method.

The third method with which we can pass headers in rest assured requests is by passing them as a List using the headers method. Below is the example of this.

Example:

List<Header> h = new ArrayList<Header>();
h.add(new Header("header1", "value1"));
h.add(new Header("header2", "value2"));
Response r = given()
.baseUri("https://www.codingninjas.com/")
.headers(h)
.get("/about/our-team.htm");

How to Retrieve Headers In Rest Assured Requests?   

Retrieve headers

When we make the API request we need to extract the response as Response. Here Response is an interface that shows the response of the request generated by the REST Assured. An example can be seen below:-


Response response= RestAssured
    .given()
    .get("https://www.codingninjas.com/");

 

OR

Response response= RestAssured
    .given()
    .get("https://www.codingninjas.com/")
    .then()
    .extract()
    .response();


The response interface gives four methods to retrieve the header.

getHeader(String headername) or header(String headerName) or  – it provides a single header value that is associated with the given name


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

REST Assured contains the methods that are only there for syntactic sugar. The above two methods are examples of that. getHeader() sounds much better than header(). We know that a header is a key-multi value pairs or key-value. If we want to retrieve a header by its name then we use getHeader(String headerName) or header(String headerName) that returns a String which is the value of that header. If we want to retrieve all headers of response then we use headers() or getHeaders() that returns Headers object.

package javaPackageName;
​
import java.util.Map;
import java.util.HashMap;
​
import io.restassured.http.Header;
import io.restassured.RestAssured;
import io.restassured.http.Headers;
import org.testng.annotations.Test;
import io.restassured.response.Response;
​
public class RestAssuredHeader {

@Test
public void xHeaderxValues() {

/*Adding Header as Key Value*/
Response objRes1 = RestAssured
.given()
.header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
.get("http://localhost:8080/tisha");

System.out.println("Method 1 -> ");
System.out.println("Content-Type: "+objRes1.contentType());
System.out.println("Status Code: "+objRes1.statusCode());

/*Adding Header as Map*/
Map<String, String> map = new HashMap<String, String>();
map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");

Response objRes2 = RestAssured
.given()
.headers(map)
.get("https://rest-assured.io/tisha");

System.out.println("Method 2 -> ");
System.out.println("Status Code: "+objRes2.getStatusCode());

Headers objHeaders1 = objRes2.getHeaders();
System.out.print(objHeaders1.get("content-encoding"));
System.out.print("\n");

/*Adding Header using Header Class*/
Header objHeader1 = new Header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
Response objRes3 = RestAssured
.given()
.header(objHeader1)
.get("https://rest-assured.io/tisha");

System.out.println("Method 3 -> ");
System.out.println("Status Code: "+objRes3.getStatusCode());

/*Retrieve Headers*/
Headers objHeaders2 = objRes3.getHeaders();
System.out.print(objHeaders2.get("Connection"));
System.out.print("\n");

/*Adding Header using Header and Header Class*/
Header objHeader2 = new Header("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
Headers objHeader3 = new Headers(objHeader2);

Response objRes4 = RestAssured
.given()
.headers(objHeader3)
.get("https://rest-assured.io/tisha");

System.out.println("Method 4 -> ");
System.out.println("Status Code: "+objRes4.getStatusCode());

/*Adding header with multiple values*/
Response objRes5 = RestAssured
.given()
.header("Accept","NextOneWillBeHeader", "TishaHeader")
.log()
.headers()
.get("https://rest-assured.io/tisha");

System.out.println("Method 5 -> ");
System.out.println("Status Code: "+objRes5.getStatusCode());

/*Retrieve Header*/
String objHeaders3 = objRes5.getHeader("Content-Type");
System.out.print(objHeaders3);
System.out.print("\n");
}
}

Output:

Output2

Frequently Asked Questions

What are headers?

Headers are the data about data of request and the response of an API. It is like as We pass the request payload in a format. It can be XML, JSON, or any other type.

How to change the default merging behavior of headers?

By default, the header value can not be overwritten if it is used multiple times. They are merged by default except “Accept” and “Content-Type”. But It can be override for the individual headers by using HeaderConfig class.

Can we pass duplicate headers?

Yes, we can pass duplicate headers and there will not be any overwritten values. For example, If you pass two values of header1 as v1 and v2 then it will be merged and passed as header1=v1 and header1=v2.

How to pass the basic authorization header in Rest Assured?

You can pass the Basic Authorization header in Rest Assured using the auth().preemptive().basic(username, password) method to authenticate API requests.

How do I blacklist a header in Rest Assured?

You can blacklist a header in Rest Assured using the headerBlacklist() method, which excludes specific headers from being logged during API request or response.

Conclusion

In this article, we have learned about headers and how to Pass or retrieve Headers In Rest Assured Requests.

Recommended read: 

Live masterclass