Last Updated On
The Content-Type field in HTTP headers indicates in which format the data is sent to, or returned by, the HTTP methods of the Rule Execution Server REST API.
The Content-type header in the request or response is added to tell the server or the browser or the medium that is serving the resource to the end user about the media type of the request or response.
Most web browsers, and API clients – e.g. Postman, Insomnia, etc are intelligent enough to add this content header to the requests/responses that you make. We need to exclusively add Content-Type while creating a test using the Rest Assured.
Test without adding Content-Type
Below is an example.
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.json.JSONObject;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
public class WithoutContentType_Demo {
@Test
public void test() {
JSONObject data = new JSONObject();
data.put("name", "William");
data.put("job", "Manager");
RestAssured
.given()
.body(data.toString())
.log().all()
.when()
.post("https://reqres.in/api/users")
.then()
.assertThat().statusCode(201)
.body("name", equalTo("William"))
.body("job", equalTo("Manager"))
.log().all();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-65.png?w=1064)
Test with Content-Type
ContentType is an enum that 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)
Let us see the use of Content-Type in an example.
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.json.JSONObject;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
public class WithContentType_Demo {
@Test
public void test() {
JSONObject data = new JSONObject();
data.put("name", "William");
data.put("job", "Manager");
RestAssured
.given()
.contentType(ContentType.JSON)
.body(data.toString())
.log().all()
.when()
.post("https://reqres.in/api/users")
.then()
.assertThat().statusCode(201)
.body("name", equalTo("William"))
.body("job", equalTo("Manager"))
.log().all();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-64.png?w=818)
We can customize the Content-type value. In the below example, we can see we have passed json as well as charset value to the header.
.header("Content-Type","application/json;charset=utf-8")
Below is a complete example.
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import org.json.JSONObject;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
public class WithContentType_Demo {
@Test
public void test() {
JSONObject data = new JSONObject();
data.put("name", "William");
data.put("job", "Manager");
RestAssured
.given()
.header("Content-Type","application/json;charset=utf-8")
.body(data.toString())
.log().all()
.when()
.post("https://reqres.in/api/users")
.then()
.assertThat().statusCode(201)
.body("name", equalTo("William"))
.body("job", equalTo("Manager"))
.log().all();
}
}
The output of the above program is
![](https://qaautomation.expert/wp-content/uploads/2023/10/image-66.png?w=877)
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!