Using extracted data in next request in Postman - Build an Automation Script
Start learning this pattern below
Jump into concepts and practice - no test required
// Login request Tests tab pm.test('Login status code is 200', function () { pm.response.to.have.status(200); }); const jsonData = pm.response.json(); pm.environment.set('authToken', jsonData.token); // Profile request Pre-request Script tab const token = pm.environment.get('authToken'); pm.request.headers.add({key: 'Authorization', value: `Bearer ${token}`}); // Profile request Tests tab pm.test('Profile status code is 200', function () { pm.response.to.have.status(200); }); pm.test('Profile response contains username testuser', function () { const profileData = pm.response.json(); pm.expect(profileData.username).to.eql('testuser'); });
In the login request's Tests tab, we check the status code is 200 to confirm success. Then we parse the JSON response to get the token and save it in an environment variable called 'authToken'.
In the profile request's Pre-request Script tab, we retrieve the saved token and add it as a Bearer token in the Authorization header. This ensures the profile request is authenticated.
In the profile request's Tests tab, we verify the status code is 200 and check that the response JSON contains the expected username 'testuser'.
This approach uses environment variables to share data between requests, which is a best practice in Postman automation.
Now add data-driven testing with 3 different user credentials for login and verify profile for each
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
