Bird
Raised Fist0
Postmantesting~8 mins

Using extracted data in next request in Postman - Framework Patterns

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
Framework Mode - Using extracted data in next request
Folder Structure
PostmanCollection/
├── collections/
│   └── api-requests.postman_collection.json
├── environments/
│   ├── dev.postman_environment.json
│   ├── staging.postman_environment.json
│   └── prod.postman_environment.json
├── scripts/
│   ├── pre-request-scripts.js
│   └── test-scripts.js
├── data/
│   └── test-data.json
└── README.md
Test Framework Layers
  • Collections: Group of API requests organized logically (e.g., user APIs, product APIs).
  • Environment Files: Store variables like base URLs, tokens, and credentials for different environments.
  • Pre-request Scripts: JavaScript code that runs before a request to set or modify variables.
  • Test Scripts: JavaScript code that runs after a request to validate response and extract data.
  • Data Files: External JSON or CSV files used for data-driven testing.
Configuration Patterns
  • Environment Variables: Use environment files to store base URLs, API keys, and tokens. Switch environments to run tests against dev, staging, or prod.
  • Global Variables: For data shared across collections or requests, like auth tokens.
  • Extract and Store Data: Use test scripts to extract data from a response and save it as environment or global variables for use in next requests.
  • Pre-request Scripts: Use these to dynamically set variables before sending requests, such as inserting extracted tokens.
Test Reporting and CI/CD Integration
  • Postman Test Results: View pass/fail status and detailed logs in the Postman Test Results tab after running collections.
  • Newman CLI: Run Postman collections from command line with Newman. Generate reports in formats like HTML, JSON, or JUnit.
  • CI/CD Integration: Integrate Newman runs into pipelines (Jenkins, GitHub Actions, GitLab CI) to automate API testing on code changes.
  • Reporting Tools: Use generated reports to track test coverage, failures, and trends over time.
Best Practices
  • Use Environment Variables: Avoid hardcoding values. Use environment variables for URLs, tokens, and credentials.
  • Extract Data Cleanly: Extract only needed data from responses and store with clear variable names.
  • Chain Requests: Use extracted variables in subsequent requests to simulate real user flows.
  • Keep Scripts Simple: Write clear and small pre-request and test scripts for maintainability.
  • Version Control Collections: Store Postman collections and environment files in version control for collaboration and history.
Self Check

Where in this folder structure would you add a script to extract a token from a login response and use it in the next API request?

Key Result
Use environment variables to extract data from one request and reuse it in the next request for chained API testing.

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