Do you think IIT Guwahati certified course can help you in your career?
No
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.
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:
Header
Description
Example
Content-Type
Specifies the format of request body
application/json
Authorization
Provides access tokens for secure APIs
Bearer ey123xyz
Accept
Defines the response format the client expects
application/json
User-Agent
Identifies client information
PostmanRuntime/7.29.0
Cache-Control
Specifies caching policies
no-cache
Cookie
Sends stored cookies
sessionId=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?
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:-
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.
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.