Using Postman, you have the capability to link multiple requests together. In Postman, a collection can be thought of as a named group of requests, typically used to group similar API requests. The requests within a collection can be arranged in a specific execution order and can be run individually or through the collection runner in Postman.
To chain multiple requests together, you can utilize the postman.setNextRequest
method and environment variables. Let's delve deeper into this concept.
Problem Statement-
While testing any API, we use to face a problem where we need to provide the Response of an API as the Request Parameter for another API. More precisely, We will learn to get a response from one API and pass it as a request parameter in another API.
In the current example, I am using 2 APIs to chain their request and responses. The first API allows to get the list of snapshots. The second API uses the snapshot id from the previous API and restores the instance using the latest snapshot. We would need to follow the following steps to achieve the same-
Step 1:
Go to Postman and Click on New -> Collection -> Add a name to your collection say API_Chaining
Step 2:
Create or add the API requests that you want to chain and save them.
Step 3:
Open the first GET request and navigate to the pre-requisite section to add the necessary details. In this case, I want to include the current date as a parameter. Here's an example of the code to add:
var moment=require('moment');
pm.environment.set('current_date',moment().format("YYYY-MM-DD"));
Next, open the Tests tab and add the following code(Make sure you replace the Request name in setNextRequest):
pm.test("Snapshot is completed and good for restore", function () {
var jsonData = pm.response.json();
let GoodSnapshot = false
for (obj of jsonData.data){
if (obj.status == "Completed"){
GoodSnapshot = true
pm.environment.set("CompletedSnapshotID", obj.snapshot_id);
return
}
}
postman.setNextRequest("Restore an instance from a snapshot");
pm.expect(GoodSnapshot).to.eql(true);
pm.response.to.have.status(200);
});
The above code checks the snapshots of the current date and passes the ID of the latest snapshot to the next request for restoration.
Make sure to add the required parameters.
Step 4:
For running the collection go to collection folder name and then click on Run:
Step 5:
- Runner will open up and it will show you the `Run Order`. Make sure the run order matches with your requirement and you need to run the requests in this order only
- You will now have to select the method to run your collection
- Run Manually
- Schedule Runs
- Automate runs via CLI - Choose the method and fill in the run configuration.
- Click on Run API_Chaining and this will execute the collection
Important Note:
Please take note that the code and query examples provided in this knowledge base article are verified as of the publication date. However, it is crucial to be aware that these examples are provided "as is" and are susceptible to variations due to potential product changes.
We strongly recommend exercising caution. While we strive to maintain accuracy, certain adjustments might be necessary to ensure compatibility with any modifications or updates introduced over time.
Stay vigilant, and kindly verify and validate the code against the latest API and Postman documentation and resources available.
Comments
0 comments
Please sign in to leave a comment.