Bird
Raised Fist0
Postmantesting~15 mins

Using extracted data in next request in Postman - Deep Dive

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
Overview - Using extracted data in next request
What is it?
Using extracted data in the next request means taking information from one API response and reusing it in a following API call. This helps create a chain of requests where each step depends on the previous one. It is common in testing workflows where you need to pass tokens, IDs, or other dynamic values between requests. This technique makes tests more realistic and automated.
Why it matters
Without using extracted data, testers would have to manually copy and paste values between requests, which is slow and error-prone. It also limits automation and makes tests fragile when data changes. Using extracted data allows tests to adapt dynamically, making them reliable and scalable. This saves time and reduces mistakes in real testing scenarios.
Where it fits
Before learning this, you should understand basic API requests and responses in Postman. You also need to know how to write simple tests and use variables. After this, you can learn about advanced scripting in Postman, environment management, and automated test suites.
Mental Model
Core Idea
Extract data from one response and reuse it as input in the next request to create a dynamic, connected test flow.
Think of it like...
It's like passing a baton in a relay race: the runner (request) hands over the baton (data) to the next runner so the race (test) continues smoothly.
┌───────────────┐      extract data      ┌───────────────┐
│ Request 1     │ ─────────────────────> │ Request 2     │
│ (response)    │                       │ (uses data)   │
└───────────────┘                       └───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding API Requests and Responses
🤔
Concept: Learn what an API request and response are and how data flows between them.
An API request is like asking a service for information or to do something. The service replies with a response containing data or status. For example, a GET request to get user info returns a response with user details in JSON format.
Result
You can see how data is sent and received between client and server.
Understanding requests and responses is essential because extracting data depends on reading the response content.
2
FoundationUsing Variables in Postman
🤔
Concept: Learn how to store and reuse values using variables in Postman.
Variables hold values that can be reused in requests. For example, you can define a variable {{userId}} and use it in the URL like /users/{{userId}}. Variables can be global, environment, or local to a collection.
Result
You can write requests that change dynamically based on variable values.
Variables are the building blocks that let you reuse extracted data in later requests.
3
IntermediateExtracting Data from Response Body
🤔Before reading on: do you think you can extract data only from JSON responses or also from other formats? Commit to your answer.
Concept: Learn how to write scripts in Postman to read and save data from the response body.
In the Tests tab, you can write JavaScript code to parse the response. For JSON, use pm.response.json() to get the object. Then pick the needed value and save it to a variable using pm.environment.set('varName', value).
Result
You can capture dynamic data like tokens or IDs from responses.
Knowing how to extract data programmatically lets you automate passing values between requests.
4
IntermediateUsing Extracted Data in Next Request
🤔Before reading on: do you think extracted data is automatically used in next requests or must be manually inserted? Commit to your answer.
Concept: Learn how to use saved variables in the next request's URL, headers, or body.
After setting a variable in Tests, use it in the next request by referencing {{varName}}. For example, if you saved a userId, you can call /users/{{userId}} in the next request URL. Postman replaces the variable with the saved value at runtime.
Result
Requests become connected and dynamic, using live data from previous responses.
Understanding variable substitution is key to chaining requests effectively.
5
AdvancedHandling Nested and Complex Data Extraction
🤔Before reading on: do you think extracting deeply nested data requires special techniques or is the same as flat data? Commit to your answer.
Concept: Learn how to extract data from nested JSON structures or arrays using JavaScript.
Use JavaScript dot notation or bracket notation to access nested fields, e.g., pm.response.json().user.details.id. For arrays, use indexing like pm.response.json().items[0].id. You can also use loops to extract multiple values.
Result
You can handle complex responses and extract exactly what you need.
Mastering nested extraction prevents errors and enables testing of real-world APIs with complex data.
6
AdvancedUsing Environment and Global Variables Wisely
🤔Before reading on: do you think environment and global variables behave the same way in all situations? Commit to your answer.
Concept: Learn the difference between environment and global variables and when to use each for extracted data.
Environment variables are tied to a specific environment and override globals. Use environment variables for data that changes per environment (dev, test, prod). Use global variables for data shared across all environments. Set variables with pm.environment.set() or pm.globals.set().
Result
You manage data scope properly, avoiding conflicts and confusion.
Knowing variable scopes helps maintain clean and reliable test setups.
7
ExpertAutomating Data Extraction with Pre-request Scripts
🤔Before reading on: do you think data extraction can only happen after a response, or can it also be prepared before a request? Commit to your answer.
Concept: Learn how to use pre-request scripts to prepare or modify variables before sending a request, enabling complex workflows.
Pre-request scripts run before a request is sent. You can use them to set or update variables based on previous data or calculations. For example, you can combine extracted data with timestamps or encode values before sending. This allows dynamic request building beyond simple substitution.
Result
Your test flows become more flexible and powerful, handling complex scenarios.
Using pre-request scripts with extracted data unlocks advanced automation and test customization.
Under the Hood
Postman runs JavaScript code in the Tests tab after receiving a response. This code can parse the response body and save values into variables stored in memory or environment files. When the next request runs, Postman replaces variable placeholders with stored values before sending the request. This process creates a chain of data passing between requests.
Why designed this way?
This design allows flexible, scriptable test flows without hardcoding values. It separates data extraction (Tests) from data usage (request building), making tests modular and maintainable. Alternatives like manual copying were error-prone and slow, so automation with scripting was chosen.
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ API Response  │──────▶│ Test Script   │──────▶│ Variable Store│
│ (JSON data)   │       │ (extract data)│       │ (save value)  │
└───────────────┘       └───────────────┘       └───────────────┘
       ▲                                               │
       │                                               ▼
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ Next Request  │◀──────│ Variable Use  │◀──────│ Variable Store│
│ (with {{var}})│       │ (replace var) │       │ (retrieve)    │
└───────────────┘       └───────────────┘       └───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Do you think variables set in one request are automatically available in all other requests without environment or global scope? Commit yes or no.
Common Belief:Variables set in one request are always available everywhere automatically.
Tap to reveal reality
Reality:Variables are only available in the scope they are set (local, environment, or global). Local variables do not persist between requests.
Why it matters:Assuming variables persist everywhere causes tests to fail because data is missing in later requests.
Quick: Do you think you can extract data only from JSON responses? Commit yes or no.
Common Belief:Data extraction only works with JSON responses.
Tap to reveal reality
Reality:You can extract data from any response format (XML, text) using appropriate parsing in scripts.
Why it matters:Limiting extraction to JSON prevents testing APIs that return other formats.
Quick: Do you think extracted variables update instantly in the same request where they are set? Commit yes or no.
Common Belief:Variables set in Tests are immediately available in the same request execution.
Tap to reveal reality
Reality:Variables set in Tests are available only in subsequent requests, not in the current request execution.
Why it matters:Expecting immediate availability leads to confusion and broken test logic.
Quick: Do you think environment variables override global variables always? Commit yes or no.
Common Belief:Global variables always override environment variables.
Tap to reveal reality
Reality:Environment variables have higher priority and override global variables if both exist with the same name.
Why it matters:Misunderstanding variable precedence causes unexpected values and test failures.
Expert Zone
1
Extracted data can be JSON objects or arrays, but storing them as variables requires stringifying or flattening, which many miss.
2
Using pm.variables.set() allows temporary variables scoped to the current request and scripts, which is different from environment or global variables.
3
Chaining requests with extracted data can cause race conditions if requests run asynchronously or in parallel, requiring careful control of execution order.
When NOT to use
Using extracted data chaining is not suitable when requests are independent or when testing isolated endpoints. In such cases, use static data or mocks instead to keep tests simple and fast.
Production Patterns
In real-world API testing, extracted data is used to authenticate sessions (extract tokens), create dependent resources (extract IDs), and validate workflows end-to-end. Teams use environment files to manage variables per environment and write reusable scripts for extraction and injection.
Connections
State Management in Frontend Development
Both involve storing and passing data dynamically between components or steps.
Understanding how extracted data flows between requests helps grasp how frontend apps manage state across user interactions.
Database Transactions
Using extracted data to chain requests is like using transaction IDs to link database operations.
Knowing this connection clarifies how data dependencies maintain consistency in both APIs and databases.
Supply Chain Logistics
Passing extracted data between requests is similar to passing shipment information between stages in a supply chain.
This cross-domain link shows how data handoffs ensure smooth process flows in both software and physical logistics.
Common Pitfalls
#1Trying to use a variable in the next request without setting it properly in the Tests script.
Wrong approach:pm.environment.set('userId', response.user.id); // missing pm.response.json() Request URL: /users/{{userId}}
Correct approach:const response = pm.response.json(); pm.environment.set('userId', response.user.id); Request URL: /users/{{userId}}
Root cause:Not parsing the response JSON before accessing its properties causes undefined values.
#2Using local variables instead of environment or global variables for data needed in next requests.
Wrong approach:pm.variables.set('token', 'abc123'); Request header: Authorization: Bearer {{token}}
Correct approach:pm.environment.set('token', 'abc123'); Request header: Authorization: Bearer {{token}}
Root cause:Local variables do not persist between requests, so the next request cannot access them.
#3Hardcoding extracted data instead of using variables, making tests brittle.
Wrong approach:Request URL: /users/12345 Tests: // no extraction or variable use
Correct approach:Tests: const response = pm.response.json(); pm.environment.set('userId', response.id); Request URL: /users/{{userId}}
Root cause:Not automating data passing causes tests to break when data changes.
Key Takeaways
Extracting data from one API response and reusing it in the next request enables dynamic, realistic test flows.
Variables in Postman store extracted data and allow substitution in URLs, headers, and bodies of subsequent requests.
Writing scripts in the Tests tab to parse responses and set variables is essential for chaining requests.
Understanding variable scopes and precedence prevents common errors in data reuse.
Advanced use of pre-request scripts and handling complex data structures unlocks powerful automated testing capabilities.

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