Make Selenium Easy

REST Assured Tutorial 51 – How To Retrieve and Assert Content-Type of Response in Rest Assured


As a part of End to End REST Assured Tutorial, in this post, we will learn how to get and assert the Content-Type of response.

Content-Type is a header that indicates the media type or MIME( Multipurpose Internet Mail Extensions ) type or type of a file. When we hit any POST or PUT API requests we may need to pass a payload. That payload can be in any supported format by API such as XML, JSON, etc. We need to use the Content-Type header to let the server know the format of the payload for a request which will be sent.

Similarly, Content-Type for response indicates the format of the response returned. It is not mandatory that the Content-Type of request and response should be the same. We may need to assert the Content-Type of response in case of a default value.

We have already covered How To Set Content-Type For Request In Rest Assured.


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

Retrieving Content-Type of a response

When we call a CRUD operation using Rest Assured, it returns a reference of the Response interface. This reference holds all data related to the response of an API. We will use the same Response reference to fetch the Content-Type of a response.

Response interface extends ResponseOptions interface and ResponseOptions interface contains below two methods to retrieve the Content-Type of a response.

  1. String contentType(); – Get the content type of the response
  2. String getContentType(); – Get the content type of the response

In fact both are same difference is only the name. Second method makes more sense than first. Rest Assured provides many syntactic sugar methods. The return type of both methods is String. If returns the value of Content-Type if present otherwise NULL.

Since Content-Type is a header, we can extract it using methods for headers as well which we discussed here.

Example Program


Asserting Content-Type of a response without extracting

In the above section, we can assert extracted Content-Type using TestNG methods or String methods. But there is a direct way to assert it directly instead of extract and then assert. We can use method content-Type() of ValidatableResponseOptions interface. We need to use then() on Response to use ValidatableResponseOptions methods.

You can download/clone the above sample project from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

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. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “REST Assured Tutorial 51 – How To Retrieve and Assert Content-Type of Response in Rest Assured

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.