Chaining request data helps you use information from one API response in the next request. This makes testing real and connected, like a conversation between apps.
Chaining request data in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
// Extract data from response and save to variable let jsonData = pm.response.json(); pm.environment.set("variable_name", jsonData.key); // Use saved variable in next request URL or body GET https://api.example.com/users/{{variable_name}}
Use pm.response.json() to parse JSON response.
Use pm.environment.set() or pm.collectionVariables.set() to save data for next requests.
userId.// Save user ID from login response let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.user.id);
userId in the URL of the next request.// Use saved userId in next request URL
GET https://api.example.com/users/{{userId}}// Save token from login response let jsonData = pm.response.json(); pm.environment.set("authToken", jsonData.token);
// Use token in Authorization header
Authorization: Bearer {{authToken}}This example shows two requests chained together. The first saves the user ID from login. The second uses that ID to get user details and checks it matches.
// First request: Login and save user ID pm.test("Login successful", function () { let jsonData = pm.response.json(); pm.environment.set("userId", jsonData.user.id); pm.expect(jsonData.user).to.have.property('id'); }); // Second request: Get user details using saved userId // URL: https://api.example.com/users/{{userId}} pm.test("User details fetched", function () { let jsonData = pm.response.json(); pm.expect(jsonData).to.have.property('id', pm.environment.get("userId")); });
Always check the response structure before extracting data to avoid errors.
Use environment or collection variables to share data between requests safely.
Chaining helps test real user flows where data depends on previous steps.
Chaining request data lets you pass info from one API call to the next.
Use pm.response.json() to get data and pm.environment.set() to save it.
This makes your tests more realistic and connected.
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'
