Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • Toggle search form

REST Assured Tutorial 67 – How to assert full response JSON body in Rest Assured?

Posted on 03/21/2025 By admin

As a part of the End to End REST Assured Tutorial, in this post, we will learn to assert a full response JSON body in Rest Assured.

If the response is kind of static or small we may want to assert the full response body directly instead of deserializing the response into some Java objects and then extract and match values.

In this post, we will learn to assert full response from an external file as well.

  io.rest-assured rest-assured 4.3.3 test

We just need to use body() method with Hamcrest Matchers. I will be using GoRest APIs for example.

package RestAssuredConcepts;

import org.hamcrest.Matchers;

import io.restassured.RestAssured;

public class AssertFullResponseBody {
        
        public static void main(String[] args) {
                
                RestAssured
                // Construct request
                .given()
                        .log()
                        .all()
                        .baseUri("https://gorest.co.in/public-api/")
                        .basePath("users/{id}")
                        .pathParam("id", "178")
                // Hit API
                .when()
                        .get()
                .then()
                // Assert response
                        .log()
                        .all()
                        .assertThat()
                        // Pass full expected response body with Hamcrest matchers
                        .body(Matchers.equalTo("{\"code\":200,\"meta\":null,\"data\":{\"id\":178,\"name\":\"Puneet Rana\","
                                        + "\"email\":\"[email protected]\",\"gender\":\"Male\",\"status\":\"Inactive\","
                                        + "\"created_at\":\"2021-02-03T03:50:07.588+05:30\",\"updated_at\":\"2021-02-03T03:50:07.588+05:30\"}}"));
        }

}

You need to convert formatted JSON in to single line and pass in equalTo() method. If you do not convert unwanted new line and carriage return (\r\n) charctaers will be present and impact validation.

We can also store expected response into an external JSON file as shown below:-

package RestAssuredConcepts;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.hamcrest.Matchers;
import io.restassured.RestAssured;

public class AssertFullResponseBodyFromExternalFile {
        
        public static void main(String[] args) throws IOException {
                
                RestAssured
                // Construct request
                .given()
                        .log()
                        .all()
                        .baseUri("https://gorest.co.in/public-api/")
                        .basePath("users/{id}")
                        .pathParam("id", "178")
                // Hit API
                .when()
                        .get()
                .then()
                // Assert response
                        .log()
                        .all()
                        .assertThat()
                        // Pass full expected response body with Hamcrest matchers
                        .body(Matchers.equalTo(new String(Files.readAllBytes(Paths.get(System.getProperty("user.dir")+"\\src\\test\\resources\\jsonFiles\\UserDetails178.json")))));
        }

}

Note – Above expected response may change when you are referring this post. Please check expected response and make changes if required.

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.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Creating Object Repository In Selenium WebDriver Without Using Any External Data Source
Next Post: List Of Frequently Asked Manual Testing Interview Questions( 200+)….

Related Posts

Hierarchy of Selenium Classes and Interfaces – Make Selenium Easy Uncategorized
ReScrollBy500 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 40 – Collection Variables in Postman Uncategorized
TestNG Tutorials 59: DataProvider in TestNG – Running DataProvider Method in Parallel – Parallel DataProvider Method Uncategorized
SelectTestngXMl – Make Selenium Easy Uncategorized

Recent Posts

  • How to Set Up Selenium WebDriver with Python Step by Step
  • How to Set Up Selenium WebDriver with Java from Scratch
  • What Is Selenium and Why It Is the Most Popular Test Automation Framework
  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark