Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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

  io.rest-assured rest-assured 4.3.1 test

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.

// Direct method to fetch Content-Type
String contentType = response.getContentType();
System.out.println("Content-Type of response is : "+contentType);

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

package RestAssuredBasicConcepts;

import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;

public class RetrieveAndAssertContentType {
        
        @Test
        public void retrieveAndAssertContentType()
        {
                Response response = RestAssured
                .given()
                .contentType(ContentType.JSON)
                .body("{\r\n" + 
                                "    \"firstname\" : \"Jim\",\r\n" + 
                                "    \"lastname\" : \"Brown\",\r\n" + 
                                "    \"totalprice\" : 111,\r\n" + 
                                "    \"depositpaid\" : true,\r\n" + 
                                "    \"bookingdates\" : {\r\n" + 
                                "        \"checkin\" : \"2018-01-01\",\r\n" + 
                                "        \"checkout\" : \"2019-01-01\"\r\n" + 
                                "    },\r\n" + 
                                "    \"additionalneeds\" : \"Breakfast\"\r\n" + 
                                "}")
                .post("https://restful-booker.herokuapp.com/booking");
                
                // Direct method to fetch Content-Type
                String contentType = response.getContentType();
                System.out.println("Content-Type of response is : "+contentType);
                
                // Since Content-Type is an header
                String contentType1 = response.getHeader("Content-Type");
                System.out.println("Content-Type of response is : "+contentType1);
        }

}
Content-Type of response is : application/json; charset=utf-8
Content-Type of response is : application/json; charset=utf-8

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.

@Test
        public void retrieveAndAssertContentType2()
        {
                RestAssured
                .given()
                .contentType(ContentType.JSON)
                .body("{\r\n" + 
                                "    \"firstname\" : \"Jim\",\r\n" + 
                                "    \"lastname\" : \"Brown\",\r\n" + 
                                "    \"totalprice\" : 111,\r\n" + 
                                "    \"depositpaid\" : true,\r\n" + 
                                "    \"bookingdates\" : {\r\n" + 
                                "        \"checkin\" : \"2018-01-01\",\r\n" + 
                                "        \"checkout\" : \"2019-01-01\"\r\n" + 
                                "    },\r\n" + 
                                "    \"additionalneeds\" : \"Breakfast\"\r\n" + 
                                "}")
                .post("https://restful-booker.herokuapp.com/booking")
                .then()
                .contentType(ContentType.JSON);
        }

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.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Singleton Design Pattern In Selenium WebDriver
Next Post: How To Launch Firefox Browser in Selenium WebDriver – Java

Related Posts

Launch of Edge and Internet Explorer browser in Selenium 3 Uncategorized
Amod Mahajan, Author at Make Selenium Easy – Page 41 of 41 Uncategorized
October 29, 2017 – Make Selenium Easy Uncategorized
August 2017 – Make Selenium Easy Uncategorized
Skipped – Make Selenium Easy Uncategorized
March 31, 2017 – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark