Back to Course
Rest Assured

Extracting Token From Login Request & Supplying In Following Requests Using Restassured

The Pattern

Many APIs require authentication: send a login request to get a token, then include that token in the header of every subsequent request.

Example

String token = given()
    .contentType("application/json")
    .body("{\"email\":\"user@test.com\",\"password\":\"pass123\"}")
.when()
    .post("/login")
.then()
    .extract().path("token");

given()
    .header("Authorization", "Bearer " + token)
.when()
    .get("/profile")
.then()
    .statusCode(200);

Why This Matters

This chaining pattern is at the core of testing almost any secured API, since nearly every protected endpoint requires a valid token obtained beforehand.

Ready to master real-world software testing?

Learn manual and automation testing hands-on with mentor-led sessions.

Explore Course