0
0
Postmantesting~10 mins

Using extracted data in next request in Postman - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test sends a first API request to get a user ID, extracts that ID from the response, and uses it in the next API request to fetch user details. It verifies that the user details response contains the correct user ID.

Test Code - Postman
Postman
pm.test("Extract user ID and use in next request", function () {
    pm.sendRequest({
        url: 'https://api.example.com/users',
        method: 'GET'
    }, function (err, res) {
        pm.expect(err).to.be.null;
        pm.expect(res).to.have.property('code', 200);
        const userId = res.json().data[0].id;
        pm.environment.set('userId', userId);

        pm.sendRequest({
            url: `https://api.example.com/users/${userId}`,
            method: 'GET'
        }, function (err2, res2) {
            pm.expect(err2).to.be.null;
            pm.expect(res2).to.have.property('code', 200);
            pm.expect(res2.json().id).to.eql(userId);
        });
    });
});
Execution Trace - 4 Steps
StepActionSystem StateAssertionResult
1Test starts and sends GET request to 'https://api.example.com/users' to get list of usersAPI server is reachable and returns a JSON list of usersResponse status code is 200PASS
2Extract the first user's ID from the response JSON and save it to environment variable 'userId'Environment variable 'userId' is set with extracted user IDExtracted user ID is not null or undefinedPASS
3Send GET request to 'https://api.example.com/users/{userId}' using extracted user IDAPI server returns user details for the specified user IDResponse status code is 200PASS
4Verify that the returned user details JSON contains the same user ID as extractedResponse JSON contains user ID matching environment variableUser ID in response equals extracted user IDPASS
Failure Scenario
Failing Condition: The first API request fails or returns no users, so user ID cannot be extracted
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of extracting the user ID in the first request?
ATo set the request method
BTo verify the response status code
CTo use it in the next request URL to get specific user details
DTo check the server availability
Key Result
Always extract dynamic data from one API response and reuse it in subsequent requests to create reliable and connected tests.