Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 50 – How to set Content-Type for request in Rest Assured

Posted on 03/21/2025 By admin

As a part of End to End REST Assured Tutorial, in this post, we will learn to why and how to set the Content-Type header in Rest Assured for a request.

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.

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

  io.rest-assured rest-assured 4.3.1 test

Postman tool automatically adds a Content-Type header based on the request body we select. For example, if select request body format as JSON then Postman will add automatically a header named “Content-Type” with value as “application/json“. This does not happen automatically in Rest Assured and you may get an unexpected response as a server may not identify the format of payload.

Let’s see an example where we are not setting up Content-Type explicitly. We are passing a payload which is in a JSON format.

package RestAssuredBasicConcepts;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class SetContentTypeForRequest {
        
        @Test
        public void WIthoutSettingContentType()
        {
                RestAssured
                .given()
                .log()
                .all()
                .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()
                .log()
                .all();
        }

}

Output

[RemoteTestNG] detected TestNG version 7.0.1
Request method: POST
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Accept=*/* Content-Type=text/plain; charset=ISO-8859-1
Cookies: 
Multiparts: 
Body:
{ "firstname" : "Jim", "lastname" : "Brown", "totalprice" : 111, "depositpaid" : true, "bookingdates" : { "checkin" : "2018-01-01", "checkout" : "2019-01-01" }, "additionalneeds" : "Breakfast"
}
HTTP/1.1 500 Internal Server Error
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: text/plain; charset=utf-8
Content-Length: 21
Etag: W/"15-/6VXivhc2MKdLfIkLcUE47K6aH0"
Date: Tue, 29 Sep 2020 07:42:56 GMT
Via: 1.1 vegur Internal Server Error
PASSED: WIthoutSettingContentType =============================================== Default test Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

We are passing a JSON payload to request but observe in output that it takes Content-Type as “text/plain” and return a response as “Internal server error“. Sever could not understand the correct format of the request payload and failed. This is the reason we should set Content-Type for a request properly.

As Content-Type is a header we can pass it as a key-value pair using methods discussed here. An example is as below:-

package RestAssuredBasicConcepts;

import org.testng.annotations.Test;

import io.restassured.RestAssured;

public class SetContentTypeForRequest {
        
        @Test
        public void settingContentTypeAsHeader()
        {
                RestAssured
                .given()
                .log()
                .all()
                .header("Content-Type", "application/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()
                .log()
                .all();
        }

}

Output

[RemoteTestNG] detected TestNG version 7.0.1
Request method: POST
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Accept=*/* Content-Type=application/json; charset=UTF-8
Cookies: 
Multiparts: 
Body:
{ "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 195
Etag: W/"c3-E5R+AIhAHdRg+t7MhfO18uLvLAw"
Date: Tue, 29 Sep 2020 07:52:19 GMT
Via: 1.1 vegur { "bookingid": 21, "booking": { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" }
}
PASSED: WIthoutSettingContentType =============================================== Default test Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

In the above approach, if you misspell the header name and its expected values then you will get an unexpected response. So to overcome that RequestSpecification class provided a below method:-

RequestSpecification contentType(ContentType contentType);

ContentType is an enum which contains members as “ANY”, “JSON”, “XML” etc. If I want to set Content -Type as JSON then I will use contentType() method as below:-

contentType(ContentType.JSON)

A complete example is as below:-

package RestAssuredBasicConcepts;

import org.testng.annotations.Test;

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

public class SetContentTypeForRequest {
        
        @Test
        public void settingContentTypeAsContentType()
        {
                RestAssured
                .given()
                .log()
                .all()
                .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()
                .log()
                .all();
        }

}

Output

[RemoteTestNG] detected TestNG version 7.0.1
Request method: POST
Request URI:    https://restful-booker.herokuapp.com/booking
Proxy: 
Request params: 
Query params:   
Form params:    
Path params:    
Headers: Accept=*/* Content-Type=application/json; charset=UTF-8
Cookies: 
Multiparts: 
Body:
{ "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast"
}
HTTP/1.1 200 OK
Server: Cowboy
Connection: keep-alive
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 195
Etag: W/"c3-Hgn0HOW7t3wbXoNXHOll0aSCOGo"
Date: Tue, 29 Sep 2020 07:58:29 GMT
Via: 1.1 vegur { "bookingid": 12, "booking": { "firstname": "Jim", "lastname": "Brown", "totalprice": 111, "depositpaid": true, "bookingdates": { "checkin": "2018-01-01", "checkout": "2019-01-01" }, "additionalneeds": "Breakfast" }
}
PASSED: settingContentTypeAsContentType =============================================== Default test Tests run: 1, Failures: 0, Skips: 0
=============================================== ===============================================
Default suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

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: Selenium Interview Question 2 – Can Selenium Be Used For API Automation?
Next Post: Postman Tutorial Part 46 – Accessing Data Variables in Pre-request and test scripts

Related Posts

YatraCal – Make Selenium Easy Uncategorized
Frequently Asked Java Program 11: Find Position Of Letter In Alphabet Uncategorized
image – Make Selenium Easy Uncategorized
Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
Introduction To Swagger API Documentation Uncategorized
API Testing Tutorial Part 5 – Safe Methods in HTTP Methods 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