← Back to libraryQuestion 237 of 468
🔌REST AssuredIntermediate

Extracting Values from a Response

📌 Definition:

REST Assured extracts values with .extract() — .extract().path('gpath') for a single value, .extract().response() for the whole response, or .extract().as(Pojo.class) — so you can reuse data in later requests or custom assertions.

📖 Detailed Explanation:

After (or instead of) inline validation, .extract() pulls data out: String token = ...post('/login').then().extract().path('token'); or int id = ...then().extract().path('data.id'). You can grab .extract().response() to inspect body, headers, cookies, and time programmatically, or .jsonPath() for a JsonPath object supporting getList/getMap. Extraction is essential for chained workflows (login → use token, create → use id) and for assertions too complex for a single matcher. It bridges REST Assured's fluent validation with plain Java logic (JUnit/TestNG asserts).

🔑 Key Points:
  • extract().path(gpath) pulls one value; as(Pojo) a typed object
  • extract().response() gives full access to body/headers/time
  • Enables chaining: login → token → next request
  • jsonPath().getList/getMap for collections
🌍 Real-World Example:

Chaining auth: String token = given().body(creds).post('/login').then().extract().path('token'); then given().header('Authorization', 'Bearer ' + token).get('/profile')... reuses the extracted token.

🎯 Scenario-Based Interview Question:

You need the created order's id from a POST response to then GET that order. How do you get and reuse it?