We use data from one response to make the next request work correctly. This helps test real workflows where one step depends on the last.
Using extracted data in next request in Postman
Start learning this pattern below
Jump into concepts and practice - no test required
// Extract data from response pm.environment.set("key", pm.response.json().field); // Use data in next request URL or body GET https://api.example.com/items/{{key}}
Use pm.environment.set or pm.variables.set to save data.
Use double curly braces {{key}} to insert saved data in next request.
// Extract token from login response pm.environment.set("authToken", pm.response.json().token);
// Use token in next request header
Authorization: Bearer {{authToken}}// Extract user ID from create user response pm.environment.set("userId", pm.response.json().id);
GET https://api.example.com/users/{{userId}}This example shows how to save a token after login, then use it in the Authorization header for the next request. It also saves a user ID after creating a user, then uses that ID in the URL to get user details.
// 1. Login request test script const jsonData = pm.response.json(); pm.environment.set("authToken", jsonData.token); // 2. Next request uses token in header // In Headers tab: Authorization: Bearer {{authToken}} // 3. Create user request test script const userData = pm.response.json(); pm.environment.set("userId", userData.id); // 4. Get user details request URL // https://api.example.com/users/{{userId}}
Always check the response structure before extracting data.
Use environment or collection variables to keep data organized.
Clear variables if you want to avoid using old data in new tests.
Extract data from one response to use in the next request.
Use pm.environment.set to save and {{variable}} to use data.
This helps test real workflows where requests depend on each other.
Practice
Solution
Step 1: Understand data dependency in workflows
In real APIs, some requests need data from previous responses to work correctly.Step 2: Recognize the role of data extraction
Extracting data lets you pass dynamic values from one request to the next, simulating real user flows.Final Answer:
To simulate real workflows where requests depend on each other -> Option DQuick Check:
Data extraction = simulate dependent requests [OK]
- Thinking extraction speeds up tests by skipping requests
- Believing extraction replaces writing tests
- Confusing extraction with random data generation
userId?Solution
Step 1: Identify correct method to set environment variable
The correct method ispm.environment.setto save a variable in environment scope.Step 2: Check syntax correctness
pm.environment.set('userId', pm.response.json().id); usespm.environment.set('userId', pm.response.json().id);which is correct syntax and usage.Final Answer:
pm.environment.set('userId', pm.response.json().id); -> Option CQuick Check:
Use pm.environment.set() to save variables [OK]
- Using pm.setEnvironment which does not exist
- Using pm.environment.save which is invalid
- Using pm.variables.set which sets local variables, not environment
pm.environment.set('token', pm.response.json().authToken);And the next request uses the header:
Authorization: Bearer {{token}}What will be the value of the Authorization header if the response JSON is
{"authToken": "abc123"}?Solution
Step 1: Extract token from response JSON
The script saves the value ofauthTokenwhich is "abc123" into environment variabletoken.Step 2: Use environment variable in next request header
The header uses{{token}}which Postman replaces with the saved value "abc123".Final Answer:
Authorization: Bearer abc123 -> Option BQuick Check:
{{token}} replaced by saved value [OK]
- Expecting {{token}} to remain as literal text
- Using wrong variable name causing empty header
- Confusing script syntax with header value
pm.environment.set('userId', pm.response.json().user.id);But the next request using
{{userId}} fails with an empty value. What is the most likely cause?Solution
Step 1: Check JSON path correctness
If the response JSON has user ID atidroot, thenpm.response.json().user.idis wrong and returns undefined.Step 2: Understand effect of wrong path
Saving undefined sets empty variable, so{{userId}}is empty in next request causing failure.Final Answer:
The JSON path is incorrect; user ID is at pm.response.json().id, not pm.response.json().user.id -> Option AQuick Check:
Correct JSON path = correct variable value [OK]
- Using wrong JSON path causing undefined variable
- Confusing pm.variables.set with pm.environment.set
- Forgetting to use {{}} in next request
- Thinking environment variables can't be used in headers
{"session": {"id": "sess789"}}Which is the correct way to extract and use this session ID in the next request URL
https://api.example.com/data/{{sessionId}}?Solution
Step 1: Extract session ID correctly from nested JSON
The session ID is atpm.response.json().session.id, so use this path to extract it.Step 2: Save to environment variable and use in URL
Usepm.environment.set('sessionId', ...)to save it, then use{{sessionId}}in the next request URL.Final Answer:
In test script: pm.environment.set('sessionId', pm.response.json().session.id); Use URL with {{sessionId}} -> Option AQuick Check:
Correct path + environment set + {{var}} usage [OK]
- Using wrong JSON path like pm.response.json().id
- Using pm.variables.set which is temporary
- Using incorrect variable name in URL
- Extracting from wrong JSON key
