Advanced Scripting For Complex Testing Workflows In Postman

Learn About Postman Advanced Scripting For Complex Testing Workflows:

In this tutorial, we will see some basic examples of using Advanced Scripting with Postman which would enable us to run complex testing workflows.

We will discuss examples like Passing data from one request to another, Request chaining i.e. controlling the executing order of the requests inside the Postman collection, etc.

=> Read Through The Easy Postman Training Series.

POSTMAN Advanced Scripting

Passing Data Between Requests In Postman

Passing data between requests is an extension of using different types of Postman variables. It’s not very uncommon where an API request depends upon data from the previous request’s response.

To build such kind of capabilities using Postman, we can simply use Postman variables to set the value depending on the response that was received from the preceding or previous requests.

Here is a Video Tutorial:

Let’s look at an example to illustrate this.

We will be using the same API with its 2 endpoints i.e. Register user and Get user details.

We will use the value of the user-id returned from the registering endpoint and use it to get the user details method. This will be achieved by storing the userId value in an environment variable and using that environment variable in the consequent request.

Please note that all such scripts will be a part of the “Tests” tab of the Postman request builder console.

Let’s see how the script will look like:

#1) For Register user endpoint, the response of this API will look as shown below.

"id": 4,
"token": "QpwL5tke4Pnpja7X4"

Fetch Data From JSON Response
Now, to execute the GET request using the user id from the response, we need to get the id value from the above response. We will use the below script to store the value of the id from this JSON into an environment variable named userId.

var jsonData = pm.response.json();

With the above script, the value of the ID will be now stored in the userId environment variable and the same can be used while executing the GET user request endpoint.

#2) Thus the GET user request will look as shown below.{{userId}}

Once the first request is executed, it will fetch the value of the UserID from the response and update the value of the environment variable, so that it could be used in the other requests.

Request Chaining In Postman

Let’s first try to understand a basic use-case of Request chaining and then we will delve further into how we can achieve Request chaining using Postman.

Consider a scenario, of the login flow of an eCommerce site and validate the logged-in user. Now for a user to log in, he or she must be first registered with the site and only then they will be able to log in. That is the order in which the actual site of the mobile app executes the API calls.

Look at it from an integration test perspective. For an API test, we first need to:

  • Call the registering endpoint of the API for the user to successfully register.
  • Then call the login endpoint and validate the details of the logged-in user.

With Postman, controlling the order of request execution is not straight forward. The default execution order is from top to bottom i.e. the order in which requests are declared or created in the Postman collection.

Request chaining or the order of request execution is changed by using the following script:


It’s important to note here that this Request chaining script needs to be added to the pre-request script or post-request script (or the tests tab in the Postman request builder) which triggers the workflow once the request under execution completes.

Here {{RequestName}} is the actual request name that is configured or set in the Postman collection.

Let’s configure a Postman collection with 4 different requests to illustrate the Request chaining concept. Create a new collection with 4 requests (We will use the same API to illustrate this).

The 4 requests in the collection will be as below:

#1) POST request to register the user ( ) with sample JSON body with fields having email and password as below.

"email": "",
"password": "pistol"

#2) POST request with login endpoint ( with the same request body as above.

#3) GET request with userID 4.

The workflow that we will try to achieve is:

Register (POST) -> User details (GET) -> Login (POST)

No matter how these requests are configured, we will create a workflow to have them executed in this order.

Postman Request Chaining

Please note that the requests are named as Register User, Get User and Login User respectively. It’s important to have these request names exactly and correctly specified in the script, else the workflow will get stuck.

Let’s see how the scripts look now.

#1) POST request to register.

postman.setNextRequest('Login User');

Register User

#2) POST request to log in.

postman.setNextRequest('Get User');

Login User Request

#3) GET request to get user details. It is important here that we don’t want anything to happen after this request. If we don’t add any workflow script here, the Postman collection is designed to resume the next request in it’s logical or default order.

Thus, suppose the original order of requests in the collection is Register, Get User and Login and our workflow is at Get User as the control does not know which request to go next, it will go to the next logical request that is Login and will ultimately result in an infinite loop.

To prevent such a situation, it’s useful to terminate the workflow using,


Get User

Whenever the above script is encountered during a collection execution, the workflow execution will stop and the collection runner will terminate.

In the cases where there are circular references of the next request, the collection runner will get stuck in an infinite loop thereby causing a memory leak sooner or later.

Please note that to execute the workflow, you will need to execute the first or starting request manually, post that, it will follow the workflow as defined in the post-request scripts.

In other words, the first request that’s required to be run as a part of the workflow should also be the first request in the collection or collection folder, so that the workflow execution starts with the first request which is also the first request of the workflow.

Advanced Workflow Chaining With Postman

The above example that we discussed, is more of a linear workflow where we just configured workflow between a set of requests in the same collection. The same technique can also be used for looping over a request multiple times based on some response values or environment variables.

Consider Integration test for a shopping cart app, where you need to test for a scenario where a user searches for a product and adds it to cart and has to perform the same operation 5 times i.e. till the cart has a total of 5 items and then finally checkout.

So, if you were to write a linear flow for this kind of test, you would’ve repeated individual requests in the collection and essentially the collection would have 5 requests for searching an item, and 5 requests for adding products to the cart and 1 request to checkout.

With this workflow functionality, we can avoid repeating the same requests in the collection and use the workflows to loop between the requests.

Let’s see a flow sequence for such a scenario (then this can be used/created using Postman workflow along with a combination of environment variables).

Complex Req Flow

There is also an interesting example of using Postman workflow to create a Spotify playlist. Refer here for more information.

Important Tips

Given below are some important tips to remember while working on building workflows.

  1. While running a collection if few requests don’t have postment.setNextRequest being set, the execution continues in the default order of collection. Thus it is generally recommended to have Postman.setNextRequest in either all requests or in none of them.
  2. When running with collections, if there are folders within a collection, then Postman.setNextRequest can be used just for requests belonging to the same folder i.e. the request selection cannot go beyond the current folder.
  3. The Postman.setNextRequest is the last statement that gets executed in the post-request or pre-request script irrespective of where and what order it’s mentioned.

In the case of multiple values found for setNextRequest, the one that’s mentioned in the last is effective.


In this tutorial, we covered few advanced scripting topics like combining environment and local variables to pass data between different requests in the Postman and how can we control the execution order of the requests using Postman Request chaining that allows advanced capabilities like looping and branching.

It’s a cool feature to mimic the behavior of an application in the way in which it would interact with different APIs and is helpful to write end to end workflows using integration tests with API endpoints.

=> Check Out The Perfect Postman Training Guide Here.