Make Selenium Easy

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


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

Why we may need to set Content-Type explicitly for a request?

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.


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.

Adding Content-Type to a request in Rest Assured

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


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


A complete example is as below:-


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

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 50 – How to set Content-Type for request in Rest Assured

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