Bird
Raised Fist0
Postmantesting~15 mins

Using extracted data in next request in Postman - Build an Automation Script

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Extract token from login response and use it in next API request
Preconditions (3)
Step 1: Send a POST request to https://api.example.com/login with JSON body {"username": "testuser", "password": "testpass"}
Step 2: Verify the response status code is 200
Step 3: Extract the 'token' value from the JSON response body
Step 4: Send a GET request to https://api.example.com/profile
Step 5: Add an Authorization header with the value 'Bearer <extracted_token>'
Step 6: Verify the response status code is 200
Step 7: Verify the response body contains the username 'testuser'
✅ Expected Result: The login request returns a token, which is used in the Authorization header of the profile request. The profile request succeeds and returns user details including username 'testuser'.
Automation Requirements - Postman (using Tests and Pre-request Scripts)
Assertions Needed:
Login response status code is 200
Token is extracted and stored in environment variable
Profile response status code is 200
Profile response body contains username 'testuser'
Best Practices:
Use pm.response.json() to parse JSON response
Store extracted token in environment variable for reuse
Use environment variable in Authorization header with Bearer scheme
Add assertions to verify status codes and response content
Keep requests independent except for data sharing via environment variables
Automated Solution
Postman
// 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.

Common Mistakes - 4 Pitfalls
Not storing the extracted token in an environment variable
Hardcoding the token value in the Authorization header
Not adding the Authorization header in the next request
Parsing response as text instead of JSON
Bonus Challenge

Now add data-driven testing with 3 different user credentials for login and verify profile for each

Show Hint

Practice

(1/5)
1. What is the main purpose of extracting data from one Postman response to use in the next request?
easy
A. To speed up the test execution by skipping requests
B. To automatically generate random data for requests
C. To avoid writing tests for each request separately
D. To simulate real workflows where requests depend on each other

Solution

  1. Step 1: Understand data dependency in workflows

    In real APIs, some requests need data from previous responses to work correctly.
  2. 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.
  3. Final Answer:

    To simulate real workflows where requests depend on each other -> Option D
  4. Quick Check:

    Data extraction = simulate dependent requests [OK]
Hint: Remember: Extract to reuse data in next request [OK]
Common Mistakes:
  • Thinking extraction speeds up tests by skipping requests
  • Believing extraction replaces writing tests
  • Confusing extraction with random data generation
2. Which Postman code snippet correctly saves a value from a response JSON to an environment variable named userId?
easy
A. pm.variables.set('userId', pm.response.json().id);
B. pm.setEnvironment('userId', pm.response.json().id);
C. pm.environment.set('userId', pm.response.json().id);
D. pm.environment.save('userId', pm.response.json().id);

Solution

  1. Step 1: Identify correct method to set environment variable

    The correct method is pm.environment.set to save a variable in environment scope.
  2. Step 2: Check syntax correctness

    pm.environment.set('userId', pm.response.json().id); uses pm.environment.set('userId', pm.response.json().id); which is correct syntax and usage.
  3. Final Answer:

    pm.environment.set('userId', pm.response.json().id); -> Option C
  4. Quick Check:

    Use pm.environment.set() to save variables [OK]
Hint: Use pm.environment.set('var', value) to save data [OK]
Common Mistakes:
  • Using pm.setEnvironment which does not exist
  • Using pm.environment.save which is invalid
  • Using pm.variables.set which sets local variables, not environment
3. Given this test script in Postman after a response:
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"}?
medium
A. Authorization: Bearer {{token}}
B. Authorization: Bearer abc123
C. Authorization: Bearer pm.response.json().authToken
D. Authorization: Bearer

Solution

  1. Step 1: Extract token from response JSON

    The script saves the value of authToken which is "abc123" into environment variable token.
  2. Step 2: Use environment variable in next request header

    The header uses {{token}} which Postman replaces with the saved value "abc123".
  3. Final Answer:

    Authorization: Bearer abc123 -> Option B
  4. Quick Check:

    {{token}} replaced by saved value [OK]
Hint: Saved variables replace {{var}} placeholders automatically [OK]
Common Mistakes:
  • Expecting {{token}} to remain as literal text
  • Using wrong variable name causing empty header
  • Confusing script syntax with header value
4. You wrote this test script to save a user ID:
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?
medium
A. The JSON path is incorrect; user ID is at pm.response.json().id, not pm.response.json().user.id
B. You must use pm.variables.set instead of pm.environment.set
C. You forgot to add double curly braces around userId in the next request
D. Environment variables cannot be used in headers

Solution

  1. Step 1: Check JSON path correctness

    If the response JSON has user ID at id root, then pm.response.json().user.id is wrong and returns undefined.
  2. Step 2: Understand effect of wrong path

    Saving undefined sets empty variable, so {{userId}} is empty in next request causing failure.
  3. Final Answer:

    The JSON path is incorrect; user ID is at pm.response.json().id, not pm.response.json().user.id -> Option A
  4. Quick Check:

    Correct JSON path = correct variable value [OK]
Hint: Verify JSON path matches response structure exactly [OK]
Common Mistakes:
  • 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
5. You want to extract a session ID from a login response and use it in the next request's URL as a path parameter. The login response JSON is:
{"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}}?
hard
A. In test script: pm.environment.set('sessionId', pm.response.json().session.id); Use URL with {{sessionId}}
B. In test script: pm.variables.set('sessionId', pm.response.json().session.id); Use URL with {{sessionId}}
C. In test script: pm.environment.set('sessionId', pm.response.json().id); Use URL with {{sessionId}}
D. In test script: pm.environment.set('sessionId', pm.response.json().sessionId); Use URL with {{sessionId}}

Solution

  1. Step 1: Extract session ID correctly from nested JSON

    The session ID is at pm.response.json().session.id, so use this path to extract it.
  2. Step 2: Save to environment variable and use in URL

    Use pm.environment.set('sessionId', ...) to save it, then use {{sessionId}} in the next request URL.
  3. Final Answer:

    In test script: pm.environment.set('sessionId', pm.response.json().session.id); Use URL with {{sessionId}} -> Option A
  4. Quick Check:

    Correct path + environment set + {{var}} usage [OK]
Hint: Match JSON path exactly and use pm.environment.set [OK]
Common Mistakes:
  • 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