Extracting Response Body data From Response Using Restassured
Extracting the Full Body
String body = given()
.when().get("/users/2")
.then().extract().asString();
Extracting a Response Object
Response response = given().when().get("/users/2");
String name = response.jsonPath().getString("data.first_name");
Why This Matters
Extracting the body lets a test store, log, or pass response data into later steps — for example, capturing a newly created resource's ID to use in a follow-up request.
Other Ways to Pull Values Out of a Response
Rest Assured gives you several extraction styles. Picking the right one keeps tests short and readable. Assume the API returns this JSON:
{
"data": {
"id": 2,
"email": "janet.weaver@example.com",
"first_name": "Janet",
"roles": ["viewer", "editor"]
}
}
Each of these lines reads from the same body:
// 1. Single value straight from the chain
int id = given().when().get("/users/2")
.then().statusCode(200)
.extract().path("data.id");
// 2. A list of values
List<String> roles = response.jsonPath().getList("data.roles");
// 3. Status, headers and timing from the same Response
int status = response.getStatusCode();
String type = response.getHeader("Content-Type");
long millis = response.getTime();
// 4. Map the JSON onto a Java class
UserWrapper user = response.as(UserWrapper.class);
System.out.println(user.getData().getEmail());
Option 4, called deserialization, needs Jackson or Gson on the classpath. It is the cleanest choice when a test checks many fields, because your IDE then offers autocomplete for every property.
A simple rule of thumb helps you choose. Use extract().path() when you need one value for the next step. Use jsonPath() when you need several values from a stored response. Use a POJO when the response is large or reused across many tests, since a change in the API then shows up as a single compile error rather than many broken string paths.
Chaining Requests with Extracted Data
Real API flows often depend on the previous response. A typical create-then-fetch test works in four steps:
- Send a POST that creates a resource.
- Assert the status code before extracting anything.
- Extract the new ID from the body.
- Use that ID as a path parameter in the next request.
String newId = given()
.contentType(ContentType.JSON)
.body("{\"name\":\"Asha\",\"job\":\"tester\"}")
.when()
.post("/users")
.then()
.statusCode(201)
.extract().path("id");
given()
.pathParam("id", newId)
.when()
.get("/users/{id}")
.then()
.statusCode(200);
Putting statusCode() before extract() matters. If the POST fails, the test stops with a clear status error instead of a confusing null pointer later on.
Filtering Arrays with GPath
Rest Assured paths use Groovy's GPath syntax, which can search inside arrays without loops. For a list endpoint returning many users:
// first user whose last name is "Lawson"
String email = response.jsonPath()
.getString("data.find { it.last_name == 'Lawson' }.email");
// all ids greater than 8
List<Integer> ids = response.jsonPath()
.getList("data.findAll { it.id > 8 }.id");
For XML responses, the same idea works through response.xmlPath().
Common Extraction Mistakes
- Wrong type: asking for a
Stringwhen the JSON holds a number causes a cast error. UsegetIntorgetStringdeliberately. - Extracting instead of asserting: if you only need to check a value,
body("data.id", equalTo(2))is shorter and gives better failure messages. - Calling the API twice: store the
Responseonce and read from it, rather than sending the same request again for each value. - Logging secrets: avoid printing full bodies that contain tokens; use
log().ifValidationFails()for debugging instead. - Brittle paths: deeply nested string paths copied from one response break when the API adds a wrapper object. Keep paths in one helper or use POJOs.
Checklist: validate status first, extract once, use typed getters or POJOs, and pass only the values the next step needs.
Ready to master real-world software testing?
Learn manual and automation testing hands-on with mentor-led sessions.