pm.test('Create user and chain to get user details', function () {
pm.sendRequest({
url: 'https://api.example.com/users',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
body: {
mode: 'raw',
raw: JSON.stringify({ name: 'John Doe', email: 'john.doe@example.com' })
}
}, function (err, res) {
pm.expect(err).to.be.null;
pm.expect(res).to.have.property('code', 201);
const userId = res.json().id;
pm.expect(userId).to.be.a('string').that.is.not.empty;
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);
const user = res2.json();
pm.expect(user).to.have.property('name', 'John Doe');
pm.expect(user).to.have.property('email', 'john.doe@example.com');
});
});
});This test script uses Postman’s pm.sendRequest to chain two API calls, simulating a real workflow.
First, it sends a POST request to create a user and checks the response status is 201 (Created).
Then, it extracts the id from the response JSON and verifies it is a non-empty string.
Next, it sends a GET request using the extracted id to retrieve user details.
Finally, it asserts the GET response status is 200 (OK) and the returned user data matches the created user.
This chaining simulates how real applications use data from one API call in the next, ensuring workflows are tested end-to-end.