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:-

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

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:-

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:-

Example:-

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.

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

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

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.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.