About this lesson
REST Assured extract(): getting a path value out of a validated response
Learn REST AssuredIntermediateJSON Path Extraction
How it works
A chain that ends in then() checks a response, but a test often needs data from it as well, for example an id for the next request. extract() switches the chain from checking to reading and returns an ExtractableResponse. From there path() reads one value, jsonPath() gives a full parser, statusCode() returns the code, and response() hands back the plain Response object.
Order matters. Every check before extract() runs first, and if one fails it throws an AssertionError, so the extraction never happens. That gives a useful guarantee: whatever you extracted came from a response that already passed its checks.
path() is generic, so its result takes the type of the variable it is assigned to. If the JSON value has another type, Java reports a ClassCastException when the value is assigned, not at compile time.
About the assignment
User 2 is the record this time. You request it, confirm the status is 200, and pull three string fields out of that validated response, printing each on its own labelled line.
What you will do
- Request
/users/2with agiven()chain and assert 200 insidethen(). - Call
extract()and keep either the whole response viaresponse()or a parser viajsonPath(). - Read
name,emailandcityfrom what you kept. - Print them labelled
Name:,Email:andCity:, in that order.
Common mistakes
Repeating the request for each field
Calling the full chain three times with three different
path()arguments sends three requests. Extract once, keep the result, and read every field from that.Assigning a number to a String
Extracting
agewithpath()into aStringvariable compiles but throwsClassCastException, because the value is anInteger. Use the right variable type orgetString().Expecting extract() to skip the checks
If the status assertion fails, nothing is extracted. The error comes from the check, so read that message before looking at your extraction code.
In a real test suite
Extraction is how API tests chain steps together: create a record, extract its id, then use it in a GET, PUT or DELETE. In JUnit 5 a helper method often wraps the request, checks and extraction, returning only the value the next step needs.
Related lessons
- BDD Style - given/when/thenThe first chain that ends in
extract().path(). - Extract Nested ValuesExtract values that sit inside nested objects.
- API Workflow - Authentication and OrderExtract a token from one response and reuse it in the next request.
To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;
public class Main {
public static void main(String[] args) {
RestAssured.baseURI = "https://testkru.com/api";
// TODO: GET /users/2, validate status 200, extract name, email, city
// TIP: You can extract the whole response first, then use jsonPath()
// OR chain .extract().path("fieldName")
}
}