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

Introduction

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.

Prerequisite

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



    io.rest-assured
    rest-assured
    4.3.1
    test

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.

// 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.

Example Program

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);
	}

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

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.

@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

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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *