0
0
Postmantesting~15 mins

Using extracted data in next request in Postman - Deep Dive

Choose your learning style9 modes available
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.