REST Assured Evaluate JSON Response Body

In this REST Assured tutorial, I am going to share with you how to evaluate the JSON content returned in the Response Body of HTTP Response.

JSON Content Returned in Response Body

Let’s say we have a RESTful Web Service endpoint that returns user details of the following structure:

{
    "userId": "Y3vWa8QwjK0DW6NmvB4HsXIV9gHg1q",
    "firstName": "Sergey",
    "lastName": "Kargopolov",
    "email": "[email protected]"
}

RESTful Web Service Endpoint

To get the above-mentioned JSON representation of user details we need to send HTTP Get request to the following URL:

Request URL:

http://localhost:8888/users/Y3vWa8QwjK0DW6NmvB4HsXIV9gHg1q

And because we might be using Spring Security, I will include the Authorization Header containing the JSON Web Token:

Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNTI5NDIwMjQzfQ.qepmpAizfH9QHNygKM-7fhhkpvJSYMUOQOTKemLeYCcy2E1yrFNxA61YCqio7rWGUmSz3KE2osqlo-7LhWzRTQ

REST Assured Test Method

Below is a working example of a Test method which:

  • Sends HTTP GET request to the above mentioned RESTful Web Service endpoint URL and includes the Authorization Header in HTTP Request,
  • Validates HTTP Response Body to check if it contains a specific String value,
  • Validates the JSON content to check if it contains a specific JSON key and a value

To validate JSON content we will use the JsonPath class which resides in the io.restassured.path.json package.

REST Assured Request Base URI

RestAssured.baseURI = "http://localhost:8888";

Prepare and Send HTTP GET Request with REST Assured

RequestSpecification httpRequest = RestAssured.given();

    // Set HTTP Headers
    httpRequest.header("Content-Type", "application/json");
    httpRequest.header("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNTI5NDIwMjQzfQ.qepmpAizfH9QHNygKM-7fhhkpvJSYMUOQOTKemLeYCcy2E1yrFNxA61YCqio7rWGUmSz3KE2osqlo-7LhWzRTQ");

    
    Response response = httpRequest.get("/users/Y3vWa8QwjK0DW6NmvB4HsXIV9gHg1q");

REST Assured Get Response Body

// Get Response Body 
ResponseBody body = response.getBody();

GET Response Body As String

// Get Response Body as String 
String bodyStringValue = body.asString();

Validate if Response Body Contains a Specific String

// Validate if Response Body Contains a specific String
Assert.assertTrue(bodyStringValue.contains("firstName"));

Validate Specific JSON Element

// Get JSON Representation from Response Body 
JsonPath jsonPathEvaluator = response.jsonPath();

// Get specific element from JSON document 
String firstName = jsonPathEvaluator.get("firstName");

// Validate if the specific JSON element is equal to expected value
Assert.assertTrue(firstName.equalsIgnoreCase("Sergey"));

REST Assured Test Method Complete Code Example

@Test
public void validateResponseJSONBody_test()
{
    RestAssured.baseURI = "http://localhost:8888";
    RequestSpecification httpRequest = RestAssured.given();

    // Set HTTP Headers
    httpRequest.header("Content-Type", "application/json");
    httpRequest.header("Authorization", "Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ0ZXN0QHRlc3QuY29tIiwiZXhwIjoxNTI5NDIwMjQzfQ.qepmpAizfH9QHNygKM-7fhhkpvJSYMUOQOTKemLeYCcy2E1yrFNxA61YCqio7rWGUmSz3KE2osqlo-7LhWzRTQ");
    
    Response response = httpRequest.get("/users/Y3vWa8QwjK0DW6NmvB4HsXIV9gHg1q");
    

    // Get Response Body 
    ResponseBody body = response.getBody();
    
    // Get Response Body as String 
    String bodyStringValue = body.asString();

    // Validate if Response Body Contains a specific String
    Assert.assertTrue(bodyStringValue.contains("firstName"));
    
    // Get JSON Representation from Response Body 
    JsonPath jsonPathEvaluator = response.jsonPath();

    // Get specific element from JSON document 
    String firstName = jsonPathEvaluator.get("firstName");

    // Validate if the specific JSON element is equal to expected value
    Assert.assertTrue(firstName.equalsIgnoreCase("Sergey"));
}

I hope this short REST Assured tutorial helped you learn how to send HTTP Get request to a RESTful Web Service endpoint and validate the content of JSON document returned inside of a Response Body of HTTP Response.

If you are interested to learn more about REST Assured I have more tutorials here: REST Assured Tutorials and if you like learning by watching step-by-step video lessons then below is a short list of video courses that teach REST Assured.

Happy learning 🙂