Chaining request data in Postman - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
/* 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.
Now add data-driven testing with 3 different user data sets for the POST request and verify each GET response accordingly.
Practice
Solution
Step 1: Understand chaining request data
Chaining request data means using data from one API response in the next request to keep tests connected.Step 2: Identify the purpose
This helps simulate real user flows where one action depends on the previous one.Final Answer:
To pass data from one API request to another for connected testing -> Option AQuick Check:
Chaining = passing data between requests [OK]
- Thinking chaining runs requests simultaneously
- Confusing chaining with changing HTTP methods
- Assuming chaining generates random data
userId?Solution
Step 1: Identify correct method to save variable
Usepm.environment.set(key, value)to save data to environment variables.Step 2: Extract value from response JSON
Usepm.response.json()to parse JSON and get theidfield.Final Answer:
pm.environment.set('userId', pm.response.json().id); -> Option DQuick Check:
Set environment variable = pm.environment.set [OK]
- Using pm.environment.get to set a variable
- Confusing pm.response and pm.environment methods
- Trying to get data from environment instead of response
const jsonData = pm.response.json();
pm.environment.set('token', jsonData.auth.token);What will be the value of the environment variable
token if the response JSON is {"auth": {"token": "abc123"}}?Solution
Step 1: Parse the response JSON
The response JSON has an object withauthcontainingtokenwith value "abc123".Step 2: Set environment variable
The script setstokenvariable tojsonData.auth.token, which is "abc123".Final Answer:
"abc123" -> Option CQuick Check:
token value = "abc123" [OK]
- Assuming token is undefined if nested
- Forgetting to parse JSON before accessing
- Expecting error when token exists
const data = pm.response.json();
pm.environment.set('userId', data.user.id);But the environment variable
userId is always empty after the request. What is the likely problem?Solution
Step 1: Check JSON structure
Ifdata.useris undefined, accessingdata.user.idreturns undefined, so variable is empty.Step 2: Confirm environment.set works correctly
pm.environment.setworks if given a valid value, so problem is likely missinguserin response.Final Answer:
The response JSON does not have auserobject -> Option BQuick Check:
Missing JSON key = empty variable [OK]
- Assuming pm.environment.set is broken
- Using wrong method like pm.variables.set
- Running script before response is received
{"users": [{"id": 1}, {"id": 2}]}You want to save the
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?Solution
Step 1: Parse response JSON correctly
The response has ausersarray with objects containingid. The first user is at index 0.Step 2: Save first user's id properly
AccessjsonData.users[0].idand save it withpm.environment.set.Final Answer:
const jsonData = pm.response.json(); pm.environment.set('firstUserId', jsonData.users[0].id); -> Option AQuick Check:
Array index 0 for first user id [OK]
- Using wrong array index or property name
- Accessing id as users.id[0] instead of users[0].id
- Using singular 'user' instead of 'users'
