0
0
Postmantesting~15 mins

Chaining request data in Postman - Build an Automation Script

Choose your learning style9 modes available
Chain two API requests using Postman
Preconditions (2)
Step 1: Send a POST request to /createUser with JSON body {"name": "John", "job": "Developer"}
Step 2: Capture the 'id' value from the response JSON
Step 3: Send a GET request to /getUserDetails using the captured 'id' as a path parameter
Step 4: Verify the GET response contains the same 'name' and 'job' as sent in the POST request
✅ Expected Result: The GET request returns user details matching the data sent in the POST request, confirming the chaining of request data works correctly.
Automation Requirements - Postman Tests (JavaScript)
Assertions Needed:
Verify POST response status is 201
Verify POST response contains an 'id' field
Verify GET response status is 200
Verify GET response 'name' equals 'John'
Verify GET response 'job' equals 'Developer'
Best Practices:
Use pm.environment variables to store and reuse data between requests
Use pm.test for assertions with clear messages
Use JSON.parse to handle response bodies
Avoid hardcoding values; use variables for flexibility
Automated Solution
Postman
/* POST /createUser request Tests tab script */
pm.test('POST status is 201', () => {
    pm.response.to.have.status(201);
});

const responseJson = pm.response.json();
pm.test('Response has id', () => {
    pm.expect(responseJson).to.have.property('id');
});

// Save id to environment variable for next request
pm.environment.set('userId', responseJson.id);

/* GET /getUserDetails request Pre-request Script */
// Use the saved userId in the request URL
const userId = pm.environment.get('userId');
pm.variables.set('userId', userId);

/* GET /getUserDetails request Tests tab script */
pm.test('GET status is 200', () => {
    pm.response.to.have.status(200);
});

const getResponse = pm.response.json();
pm.test('Name is John', () => {
    pm.expect(getResponse.name).to.eql('John');
});

pm.test('Job is Developer', () => {
    pm.expect(getResponse.job).to.eql('Developer');
});

The POST request test script first checks the response status is 201, which means the user was created successfully. It then parses the JSON response and asserts that an 'id' field exists. This 'id' is saved to an environment variable userId for use in the next request.

In the GET request, the pre-request script retrieves the saved userId and sets it as a variable to be used in the request URL path parameter.

The GET request test script verifies the response status is 200, meaning the user details were fetched successfully. It then asserts that the returned 'name' and 'job' fields match the values sent in the POST request, confirming the chaining of data between requests.

This approach uses Postman's environment variables to pass data between requests, and pm.test with clear messages to make debugging easier.

Common Mistakes - 3 Pitfalls
{'mistake': "Not saving the 'id' from the POST response to an environment variable", 'why_bad': "Without saving the 'id', the GET request cannot use it to fetch the correct user details, breaking the chain.", 'correct_approach': "Use pm.environment.set('userId', responseJson.id) to save the 'id' after the POST request."}
{'mistake': 'Hardcoding the userId in the GET request URL instead of using variables', 'why_bad': "Hardcoding makes the test inflexible and fails if the 'id' changes or is dynamic.", 'correct_approach': 'Use a variable like {{userId}} in the GET request URL and set it dynamically in scripts.'}
Not checking response status codes before accessing response data
Bonus Challenge

Now add data-driven testing with 3 different user data sets for the POST request and verify each GET response accordingly.

Show Hint