Make Selenium Easy

REST Assured Tutorial 49 – How To Retrieve Single and MultiValue Headers From Response Using Rest Assured

Introduction

As a part of End to End REST Assured Tutorial, in this post, we will learn to retrieve header values from response.

When we send a request to a server using an API, the server sends some headers along with the response. We may need to assert those response headers as part of testing. To assert header values we need to get headers from the response which we will learn in this post.

Note here that I am talking about Response headers not request headers. You have control over request headers except for some auto-generated headers. You can easily retrieve headers added by us to a request.

Topics you will learn from this post

  1. Retrieve single value headers from the response
  2. Retrieve multivalue headers from the response
  3. Usage of getHeaders(), getHeader(String headerName), headers(), header(String headerName)
  4. How to assert if a header is present in the response headers?

Prerequisite

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

Retrieve headers from the response

Once you hit the API request you need to extract the response as Response. Note here that Response is an interface that represents the response of a request made by REST Assured. An example is below:-

OR

The response interface provides four methods(effectively two) to retrieve a header and headers.

header(String headerName) or getHeader(String headername) – Get a single header value associated with the given name.

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

REST Assured contains some methods that are only there for syntactic sugar. The two methods above are examples of that. getHeader() sounds better than header().

Since we know that a header is a key-value or key-multivalue pairs. So if you want to retrieve a specific header by its name then use getHeader(String headerName) or header(String headerName) who returns a String which is actually the value of that header. If you want to retrieve all headers of response then use headers() or getHeaders() who return Headers object.

As we know that a header may be a multivalue header as well in that case you need to twist a bit to retrieve all values or a list of values of a given header as shown below:-

How to assert if a header is present in the response

If we try to retrieve a non-existing header from the response it returns NULL. We can have a user-defined method that can be used to find if a header is present in response based on this behaviour.

But there is a direct method already available named hasHeaderWithName(String headerName). It returns true if the header is present otherwise false.

If you use getHeaders() then also you can retrieve header values using get() or getValue() as shown 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

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning

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.

REST Assured Tutorial 49 – How To Retrieve Single and MultiValue Headers From Response Using Rest Assured

by Amod Mahajan time to read: 3 min
0
%d bloggers like this: