This below code snippet demonstrates how to use data from one API response and reuse it in a subsequent request. It showcases a common authentication workflow where a token received from a login API is passed in the Authorization header to access a secured endpoint using RestAssured in Java.
🧪 Use Case:
This is a basic example of token-based authentication testing using REST APIs. It's useful in:
API automation frameworks
Smoke tests for authentication flows.
Demonstrating dynamic data chaining between API calls
Login Request (POST /api/login)
Sends a JSON payload with email and password to Reqrest.
Validates that:
- Status code is 200 OK.
- Response body contains a non-null "token".
- Extracts the token from the response.
Authenticated Request (GET /api/users/2)
Adds the extracted token to the Authorization header in the format:
Authorization: Bearer <token>
- Sends a GET request to fetch user details.
- Validates that the response status is 200 OK.
- Prints the formatted JSON response.