You can also do like this if you're only interested in extracting the "user_id":
String userId =
given().
contentType("application/json").
body(requestBody).
when().
post("/admin").
then().
statusCode(200).
extract().
path("user_id");
In its simplest form it looks like this:
String userId = get("/person").path("person.userId");
Answer from Johan on Stack OverflowTOOLSQA
toolsqa.com › rest-assured › read-json-response-body-using-rest-assured
How to Read Json Response Body using Rest Assured?
In this tutorial, we will learn about How to Read JSON Response Body using Rest Assured? and How to Validate Content of a Response Body? Let us continue with the example of Weather web service that we used in the previous tutorials. When we request for the Weather details of a particular city, Server responds by sending the Weather details of the city as the Response Body. Response interface contains two methods to get the Response Body
Top answer 1 of 6
52
You can also do like this if you're only interested in extracting the "user_id":
String userId =
given().
contentType("application/json").
body(requestBody).
when().
post("/admin").
then().
statusCode(200).
extract().
path("user_id");
In its simplest form it looks like this:
String userId = get("/person").path("person.userId");
2 of 6
31
I found the answer :)
Use JsonPath or XmlPath (in case you have XML) to get data from the response body.
In my case:
JsonPath jsonPath = new JsonPath(responseBody);
int user_id = jsonPath.getInt("user_id");
Videos
22:37
12. How to Read and Validate JSON Response Body using Rest Assured ...
05:35
How to Validate Response Body Values using Rest Assured - YouTube
11:56
#8 How to Read Response Body using JSONPath | JSONPATH with Rest ...
08:16
16. Extract Response Body As String In Rest Assured - YouTube
06:17
REST Assured Tutorial #8 - Response Body in Rest Assured - YouTube
02:14
REST Assured Tutorial #10 - Verify Response Body in Rest Assured ...
Javadoc.io
javadoc.io › doc › io.rest-assured › rest-assured › 3.0.1 › io › restassured › response › Response.html
Response - rest-assured 3.0.1 javadoc
Bookmarks · Latest version of io.rest-assured:rest-assured · https://javadoc.io/doc/io.rest-assured/rest-assured · Current version 3.0.1 · https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.1 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc...
Javadoc.io
javadoc.io › doc › com.jayway.restassured › rest-assured › 1.6.1 › com › jayway › restassured › response › ResponseBody.html
ResponseBody - rest-assured 1.6.1 javadoc
Bookmarks · Latest version of com.jayway.restassured:rest-assured · https://javadoc.io/doc/com.jayway.restassured/rest-assured · Current version 1.6.1 · https://javadoc.io/doc/com.jayway.restassured/rest-assured/1.6.1 · package-list path (used for javadoc generation -link option) · ...
Apps Developer Blog
appsdeveloperblog.com › home › java › validate json response with rest assured
Validate JSON Response with REST Assured - Apps Developer Blog
March 23, 2024 - Validates the 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 value. NOTE: To validate the JSON content, we will use the JsonPath class, which is located in the io.restassured.path.json package.
Makeseleniumeasy
makeseleniumeasy.com › 2021 › 02 › 03 › rest-assured-tutorial-67-how-to-assert-full-response-json-body-in-rest-assured
REST Assured Tutorial 67 – How to assert full response JSON body in Rest Assured?
February 3, 2021 - 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"))))); } }
Apps Developer Blog
appsdeveloperblog.com › home › java › rest assured get http response body
REST Assured Get HTTP Response Body - Apps Developer Blog
May 15, 2018 - In this short tutorial on REST Assured you will learn how to get the entire HTTP Response Body when testing your RESTful Web Service Endpoint.
DevQA
devqa.io › parse-json-response-rest-assured
How to Parse JSON Response with REST-assured - DevQA.io
then().contentType(ContentType.JSON).extract().response(); } public static void main(String[] args) { Response response = doGetRequest("https://jsonplaceholder.typicode.com/users"); List<String> jsonResponse = response.jsonPath().getList("$"); System.out.println(jsonResponse.size()); } } The result of the above call would print 10. Note the $ notation which means the root element. In the above example, if we wanted to get the username of all entries, we could use:
TutorialsPoint
tutorialspoint.com › how-to-verify-a-json-response-body-using-assertions-in-rest-assured
How to verify a JSON response body using Assertions in Rest Assured?
Using Rest Assured, we shall verify the value of the Location in the Response body. ... import org.hamcrest.Matchers; import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; public class NewTest { @Test public void ressponseAssertion() { //base URL RestAssured.baseURI = "https://run.mocky.io"; //GET operation given() .when().get("/v3/6c6ed634-5e78-4b80-94c7-cf17c04c7055").
Javadoc.io
javadoc.io › doc › io.rest-assured › rest-assured › 3.0.6 › io › restassured › response › ResponseBody.html
ResponseBody - rest-assured 3.0.6 javadoc
Latest version of io.rest-assured:rest-assured · https://javadoc.io/doc/io.rest-assured/rest-assured · Current version 3.0.6 · https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.6 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/io.rest-assured/rest-assured/3.0.6/package-list ·
TutorialsPoint
tutorialspoint.com › how-to-extract-the-whole-json-response-as-a-string-in-rest-assured
How to extract the whole JSON response as a string in Rest Assured?
import org.testng.annotations.Test; import static io.restassured.RestAssured.given; import io.restassured.RestAssured; public class NewTest { @Test public void getResponseAsString() { //base URL RestAssured.baseURI = "https://run.mocky.io/v3"; String r = RestAssured.given().when() //get request .get("/cd3a7e12-9057-4b51-bf2d-a2d4e2ddad8d") //get response as string .then().extract().response().asString(); System.out.println(r); } }
GitHub
github.com › rest-assured › rest-assured › wiki › usage
Usage · rest-assured/rest-assured Wiki · GitHub
REST Assured is a Java DSL for simplifying testing of REST based services built on top of HTTP Builder. It supports POST, GET, PUT, DELETE, OPTIONS, PATCH and HEAD requests and can be used to validate and verify the response of these requests.
Author rest-assured