Last Updated On
In this tutorial, we will discuss how to validate the HTTP response status code, status like, header, body and Content Type using REST Assured. Every HTTP Response received from a server in response to an HTTP request sent by the client has a status code.
Add the Rest Assured dependency in the pom.xml.
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>5.5.0</version>
<scope>test</scope>
</dependency>
Verifying HTTP Response Status Code with Rest Assured
A response code indicating the status of the request (e.g., 200 OK, 404 Not Found). The status code that the server returns tells us whether the request was successful or not. If the request was successful, the server sends the status code in the range of 200-299. If the request was not successful, then the status code other than the range is returned.
The getStatusCode() method returns an integer and then we can verify its value.
Below is an example.
@Test
public void verifyStatusCode() {
String BaseURL = "https://dummy.restapiexample.com/api";
// GIVEN
Response response = given()
// WHEN
.when()
.get("https://reqres.in/api/users/2")
// THEN
.then()
.extract().response();
int actualStatusCode = response.getStatusCode();
System.out.println("Status Code : " + actualStatusCode);
Assert.assertEquals(200, actualStatusCode);
}
The output of the above program is

Verifying HTTP Response Status Line with Rest Assured
The getStatusLine() method returns a string and then we can verify its value.
@Test
public void verifyStatusLine() {
// GIVEN
Response response = given()
// WHEN
.when()
.get("https://reqres.in/api/users/2")
// THEN
.then()
.extract().response();
String actualStatusLine = response.getStatusLine();
System.out.println("Status Line : " + actualStatusLine);
Assert.assertEquals("HTTP/1.1 200 OK", actualStatusLine);
}
The output of the above program is

Verifying HTTP Response Body with Rest Assured
In the below example, we are obtaining the body content of the HTTP response using the getBody().
We have used the JsonPath to query the JSON body of the response. To know more about the JsonPath, please refer this tutorial – Extraction from JSON in Rest Assured – JsonPath.
@Test
public void verifyResponseBody() {
// GIVEN
Response response = given()
// WHEN
.when()
.get("https://reqres.in/api/users/2")
// THEN
.then()
.extract().response();
String actualResponseBody = response.getBody().asString();
System.out.println("Response Body : " + actualResponseBody);
JsonPath jsonPath = response.jsonPath();
String actualResponse_Id = jsonPath.getString("data.id");
System.out.println("Response Id : " + actualResponse_Id);
Assert.assertEquals("2", actualResponse_Id);
}
The output of the above program is

Verifying HTTP Response Header with Rest Assured
getHeaders() method is used to retrieve all the headers from a HTTP response.
@Test
public void verifyResponseHeader() {
// GIVEN
Response response = given()
// WHEN
.when()
.get("https://reqres.in/api/users/2")
// THEN
.then()
.extract().response();
Headers allHeaders = response.getHeaders();
System.out.println("All Headers : " + allHeaders);
for(Header header: allHeaders){
System.out.println(header.getName() + ":" + header.getValue());
}
Assert.assertTrue(allHeaders.getValue("Content-Encoding").contains("gzip"));
}
The output of the above program is

Verifying HTTP Response Content Type with Rest Assured
getContentType() retrieves the “Content-Type” of the header of the response body.
@Test
public void verifyContentType() {
// GIVEN
Response response = given()
// WHEN
.when()
.get("https://reqres.in/api/users/2")
// THEN
.then()
.extract().response();
String actualContentType = response.getContentType();
System.out.println("Actual ContentType : " + actualContentType);
Assert.assertEquals("application/json; charset=utf-8", actualContentType);
}
The output of the above program is

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!