How To Use Postman Response In Next Request
Utilizing Postman Response in Subsequent Requests
Postman is a powerful tool for API testing, and one of its key strengths lies in its ability to automate tests and chain requests together. This guide will explore how to leverage Postman’s robust features to use the response from one request as input for another request, streamlining your testing workflow and improving efficiency.
1. Dynamically Extracting Values from Responses
Scenario: You need to access a specific piece of data from a previous request’s response and use it as a parameter in the next request.
Example:
- Request 1: Get a list of users (GET /users)
- Request 2: Retrieve details for a specific user by its unique ID (GET /users/{userId})
Step 1: Define a Variable
In your first request (getting the list of users), you need to define a variable to store the extracted data.
- Go to the “Tests” tab of your first request.
- Write a test script to extract the desired data:
var userId = pm.response.json().data[0].id;pm.variables.set("userId", userId);
This snippet assumes your response is in JSON format. It retrieves the first user’s ID and stores it in a Postman variable named userId
.
Step 2: Utilize the Variable in the Subsequent Request
- Navigate to the second request (getting user details).
- In the URL, replace
{userId}
with{{userId}}
: This instructs Postman to use the value stored in theuserId
variable.
Example:
- Request 1 URL:
/users
- Request 2 URL:
/users/{{userId}}
Now, Postman will dynamically fetch the userId
from the variable and use it to make the second request, effectively chaining the requests.
2. Passing Data Through Environment Variables
Scenario: You need to consistently reuse the same data across multiple requests in your collection.
Example:
- Request 1: Login to your application (POST /login)
- Request 2, 3, 4…: Use the retrieved authentication token in subsequent requests that require authorization.
Step 1: Store Token in an Environment Variable
- In your first request’s “Tests” tab, extract the authentication token:
var authToken = pm.response.json().token;pm.environment.set("authToken", authToken);
This will save the retrieved token to the environment variable authToken
.
Step 2: Utilize the Environment Variable
- Navigate to the requests requiring authorization.
- Include the token in the request header:
Authorization: Bearer {{authToken}}
Postman will automatically substitute the {{authToken}}
with the value stored in the environment variable.
Note: You can manage environment variables in the Postman interface under the “Environments” section.
3. Passing Data Through Global Variables
Scenario: You need to share data across multiple collections (a collection is a group of related requests).
Example:
- You have a
User Management
collection and anOrder Management
collection. - You need to use the same base URL across both collections.
Step 1: Set a Global Variable
- Open the Global Variables section in Postman (from the settings menu).
- Add a new global variable: For example, name it
baseUrl
and set its value to the base URL of your API.
Step 2: Utilize the Global Variable
- In each collection’s requests, refer to the
baseUrl
variable:
{{baseUrl}}/users{{baseUrl}}/orders
This allows you to change the base URL centrally in the global variables and have it reflected in all your collections.
4. Using the pm.sendRequest
Function
Scenario: You need to make an asynchronous request within another request’s tests.
Example:
- Request 1: Get a list of products.
- Request 2 (within Request 1’s “Tests” tab): Get the price of the first product.
Step 1: Use pm.sendRequest
:
pm.sendRequest({ url: 'https://api.example.com/products/{{productId}}', // Assuming product ID was extracted previously method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer {{authToken}}' // If required }}, (error, response) => { if (error) { console.error(error); } else { var productPrice = response.json().price; console.log("Product price:", productPrice); }});
This code makes a GET request to the product details endpoint and then uses the response to log the product price to the console.
Note: This approach provides flexibility for handling asynchronous operations within your requests.
5. Chaining Requests Using Collection Runners
Scenario: You need to execute a series of requests automatically in a specific order.
Example:
- You have a collection that simulates a user registration and login flow.
Step 1: Create a Collection
Combine all your requests related to the registration and login process into a single collection.
Step 2: Configure the Collection Runner
- Open the collection runner by clicking the play button next to your collection.
- Choose an environment for your variables.
- Configure iteration settings: If you need to run the collection multiple times, you can adjust the iterations and data file.
Step 3: Run the Collection
Click the “Run” button and watch as Postman automatically executes your requests in the specified order, processing the results smoothly.
Note: You can also set up pre-request scripts and test scripts within each request to further customize the behavior of the collection runner.