Test Overview
This test sends two API requests in Postman. It verifies that data from the first response is correctly used in the second request, ensuring chaining of request data works.
Jump into concepts and practice - no test required
This test sends two API requests in Postman. It verifies that data from the first response is correctly used in the second request, ensuring chaining of request data works.
pm.test("Chaining request data test", function () { // First request: Get user info pm.sendRequest("https://jsonplaceholder.typicode.com/users/1", function (err, res) { pm.expect(err).to.be.null; pm.expect(res).to.have.property('status', 200); const userId = res.json().id; pm.expect(userId).to.eql(1); // Second request: Get posts by userId from first response pm.sendRequest({ url: `https://jsonplaceholder.typicode.com/posts?userId=${userId}`, method: 'GET' }, function (err2, res2) { pm.expect(err2).to.be.null; pm.expect(res2).to.have.property('status', 200); const posts = res2.json(); pm.expect(posts.length).to.be.greaterThan(0); // Check that all posts belong to userId posts.forEach(post => { pm.expect(post.userId).to.eql(userId); }); }); }); });
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Postman test runner is ready to execute the test script | — | PASS |
| 2 | Send first GET request to https://jsonplaceholder.typicode.com/users/1 | Browser/Postman sends HTTP GET request to user API endpoint | Check response status code is 200 | PASS |
| 3 | Parse JSON response and extract userId | Response body contains user data with id=1 | Verify userId equals 1 | PASS |
| 4 | Send second GET request to https://jsonplaceholder.typicode.com/posts?userId=1 using extracted userId | Postman sends HTTP GET request to posts API with userId query parameter | Check response status code is 200 | PASS |
| 5 | Parse JSON response and verify posts belong to userId=1 | Response body contains array of posts with userId=1 | Verify posts array length > 0 and each post.userId equals 1 | PASS |
| 6 | Test ends successfully | All assertions passed, test completes | — | PASS |
userId?pm.environment.set(key, value) to save data to environment variables.pm.response.json() to parse JSON and get the id field.const jsonData = pm.response.json();
pm.environment.set('token', jsonData.auth.token);token if the response JSON is {"auth": {"token": "abc123"}}?auth containing token with value "abc123".token variable to jsonData.auth.token, which is "abc123".const data = pm.response.json();
pm.environment.set('userId', data.user.id);userId is always empty after the request. What is the likely problem?data.user is undefined, accessing data.user.id returns undefined, so variable is empty.pm.environment.set works if given a valid value, so problem is likely missing user in response.user object -> Option B{"users": [{"id": 1}, {"id": 2}]}id of the first user to an environment variable firstUserId and use it in the second request URL as /users/{{firstUserId}}. Which script correctly does this in the first request's Tests tab?users array with objects containing id. The first user is at index 0.jsonData.users[0].id and save it with pm.environment.set.