REST Assured Tutorial
Read JSON Response Body
How To Read JSON Response Body ?
When we request to the server then server reponds by sending information which is Response Body for that particular request. Let's continue with the previous example of userProfile web service - Rest Assured provides an interface named 'ResponseBody' that contains two methods as :
body() : returns ResponseBody
getBody() : returns ResponseBody
*Both methods do exactly the same thing. Using these methods we can get an object of type ResponseBody :
ResponseBody body=response.getBody();
In below example code, we will simply use getBody() to read response body and to print the same :
package apiTesting;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class ReadJSONResponseBody {
@Test
public void readJSONResponseBody() {
RestAssured.baseURI = "http://localhost:8080/msg/webapi/userProfiles";
RequestSpecification httpRequest = RestAssured.given();
io.restassured.response.Response response = httpRequest.get("");
ResponseBody body=response.getBody();
System.out.println("Response Body Is ==> "+body.asString());
}
}
Output :
Response Body Is ==> [{"created":"2022-05-14T09:26:16.562Z[UTC]","firstName":"Bella","id":1,"lastName":"Gilbert","profileName":"Bella Profile"},{"created":"2022-05-14T09:26:16.562Z[UTC]","firstName":"Elena","id":2,"lastName":"Swan","profileName":"Elena Profile"},{"created":"2022-05-14T09:26:16.562Z[UTC]","firstName":"Caroline","id":3,"lastName":"Marshall","profileName":"Caroline Profile"},{"created":"2022-05-14T09:26:16.562Z[UTC]","firstName":"Sarah","id":4,"lastName":"Humpton","profileName":"Sarah Profile"}]
Validate Response Body Contains Some String
Response body can return the response in String format by using String methods. To validate the presence of a particular string in response body, we can use a String method 'contains()' on it :
body.asString().contains("Sarah")
Here's an example of this :
package apiTesting;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;
public class ValidateResponseBodyContainsSomeString {
@Test
public void validateResponseBodyContainsSomeString() {
RestAssured.baseURI = "http://localhost:8080/msg/webapi/userProfiles";
RequestSpecification httpRequest = RestAssured.given();
io.restassured.response.Response response = httpRequest.get("");
ResponseBody body=response.getBody();
Assert.assertTrue(body.asString().contains("Sarah"), "Response Body doesn't Contain Sarah");
}
}
Output :
[RemoteTestNG] detected TestNG version 6.14.3
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
PASSED: validateResponseBodyContainsSomeString
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Extract A Node Using Json Path
Import JsonPath : import io.restassured.path.json.JsonPath;
Create object for it : JsonPath jsonpath=response.jsonPath();
Query for a particular node : String firstName=jsonpath.getString("firstName");
Here's an example of this :
package apiTesting;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.specification.RequestSpecification;
public class ExtractTheNode {
@Test
public void extractTheNode() {
RestAssured.baseURI = "http://localhost:8080/msg/webapi/userProfiles";
RequestSpecification httpRequest = RestAssured.given();
io.restassured.response.Response response = httpRequest.get("");
JsonPath jsonpath=response.jsonPath();
String firstName=jsonpath.getString("firstName");
System.out.println("Received Node Are : "+firstName);
}
}
Output :
[RemoteTestNG] detected TestNG version 6.14.3 [TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected. Received Nodes Are : [Bella, Elena, Caroline, Sarah] PASSED: extractANode =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 ===============================================