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.
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.
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); }); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and sends GET request to 'https://api.example.com/users' to get list of users | API server is reachable and returns a JSON list of users | Response status code is 200 | PASS |
| 2 | Extract the first user's ID from the response JSON and save it to environment variable 'userId' | Environment variable 'userId' is set with extracted user ID | Extracted user ID is not null or undefined | PASS |
| 3 | Send GET request to 'https://api.example.com/users/{userId}' using extracted user ID | API server returns user details for the specified user ID | Response status code is 200 | PASS |
| 4 | Verify that the returned user details JSON contains the same user ID as extracted | Response JSON contains user ID matching environment variable | User ID in response equals extracted user ID | PASS |