How to Validate REST API Request, Response Using Rest Assured

Diksha Gupta
5 min readMay 8, 2023

--

In the world of software development, it’s essential to ensure that our applications behave as expected. One critical aspect of this is validating HTTP status codes returned by our REST APIs. This article will explore how to use Rest Assured, a popular Java-based testing framework, to validate HTTP status codes.

How to Validate HTTP REST API Response

When a client sends a RESTful request to the server, in return the server sends the response back to the client with the requested resource.

Rest Assured provides a simple and effective way to validate HTTP status codes.

The server response contains following

  1. Status Code
  2. Error Message(If any)
  3. Response body
  4. Headers

In the previous article, we have seen our first REST API test, and now we will learn about its response and other validations.

Validating HTTP status codes is one of the most critical aspects of testing REST APIs. HTTP status codes indicate the outcome of a request, such as success or failure. Rest Assured provides a simple way to validate HTTP status codes using the statusCode() method.

When we talk about validation, one thing comes into our mind: we would have a mechanism, through which we read the entire response object and then validate the status code, response body, and other components.

The server sends the API request response in JSON or XML form. This depends on the Media-Type we pass in the API request call.

Also, the response from the server contains Headers, which are called response headers. We will get to know the format of the response from the Content-Type header of the response.

Let’s take an example of the same API which we discussed in our previous article.

When we hit this request, we receive the below response.

As we see in the above screenshot, the response has a status, headers, and body. It has a content-type attribute that has the value, on validating this header, the client knows what would be the response format.

Let us now proceed with validating the status part of the response.

Validating the Status Code

HTTP status codes are three-digit codes sent by a server to indicate the status of a requested resource. These codes are grouped into five classes, each with a specific meaning ranging from 1xx- 5xx

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 other status code is returned. You can check the list of most commonly used HTTP status codes along with their description in my previous article HTTP Status Codes

Rest Assured provides a package named “io.restassured.response” which has a Response interface. The Response interface provides methods that can help to manipulate the parts of the received response. The following screenshot shows some of the important methods of the response interface.

When we use the getStatusCode() method, then this will return an integer value, that we can use to validate the response status of the server response. Let’s see its practical use

When we run this Test case, we get below output

Basically, we are using this method to extract the response code

int statusCode= response.getStatusCode();

Then, we are comparing the received status code against the expected value using the below command.

Assert.assertEquals(statusCode,HttpStatus.SC_Ok);

Validating Status code for the Error Scenarios

Till this point, we have discussed the happy flow, the client sends an API request, and in return server returns a successful response with a 200 Status code. But this is not always the case, what if the server we send a request to is not available or does not return a response within the stipulated time, or We do a mistake in passing the address of the resource.

When any of the above scenarios occur, the REST API will return an appropriate status code other than 200. The client in turn has to validate this status code and process it accordingly.

Let’s discuss now how the API will behave and what response it will return in these cases.

Case 1: Here I have intentionally changed the resource address and passed it as “/searchh” instead of “/search”.

If I run this Test, then we receive the below result.

Here, We see that the test returns a status code of 404 but we expected 200. Hence we received an AssertionError. Also, an appropriate Error message is also returned.

Case 2: Now I will use the same code but with the POST HTTP method instead of GET

When we run this test, We see the result as:

Conclusion

Validating HTTP status codes is an essential part of testing REST APIs. Rest Assured provides a simple and expressive way to validate HTTP status codes using the statusCode() method. By using Rest Assured, you can write concise and readable tests that ensure your REST APIs behave as expected.

In the next article, we will discuss more on validating the response. I hope you liked the article.

Keep sharing and Happy Learning !! DG

--

--

Diksha Gupta

Sr. Automation QA at top Payment and Technology Company In Dubai.