Postman How-to: Dynamic Iteration Within a Collection

Postman How-to: Dynamic Iteration Within a Collection

You might be familiar with using the Postman Collection Runner to run a collection over multiple variables using a CSV file. But what about situations where you don’t know the data in advance? For example, let’s say you want to test an item info API with all the current items. First you need to get the list of items, then call the info API with each item. So you don’t want to loop the entire collection, just the item info request. The postman.sendNextRequest function can help you do just that.

I’ve made a simple collection using the World Time API to demonstrate this, available here. The collection gets a list of time zones, then gets the time information for each time zone in the Africa region.

Getting the initial list

The first request, Get Time Zones, calls http://worldtimeapi.org/api/timezone to get a list of all the available time zones.

GetTimeZones

Now we can start processing this response in the Tests tab. There are nearly 400 time zones and the API is rate-limited, so narrowing our requests to African time zones will enable us to do a complete loop without getting locked out.

// This is the timezone array
var tz = pm.response.json();

// Fresh array for the filtered zones
var zones = []

// Note that 'z' is an index number that
// is used to fetch the actual string
// from the array
for (var z in tz){
    var zone = tz[z]
    if (zone.startsWith("Africa")){
        zones.push(zone)
    }
}

Once we have our subset, we then assign it to a collection variable along with the starting index. This will enable the next request to use the data.

pm.variables.set("timezones", JSON.stringify(zones))
pm.variables.set("tz_index", 0)

Looping Through the List

Now comes the fun part! Our next request, Get Time For Zone, has a URL of http://worldtimeapi.org/api/timezone/{{tz}}, with the tz variable being set in the Pre-request tab:

// Fetch the variables set in "Get Time Zones" 
// and get the next zone in the list

tz = JSON.parse(pm.variables.get('timezones'))
index = parseInt(pm.variables.get('tz_index'))
pm.variables.set('tz', tz[index])

Also in the Pre-request tab is the secret sauce that brings the whole collection together:

// If we are *not* on the last item in the list,
// increment the index and set the next request 
// back to this one. Otherwise end the loop.

if (index + 1 < tz.length){
    pm.variables.set('tz_index', index + 1)
    postman.setNextRequest('Get Time For Zone')
}else{
    postman.setNextRequest(null)
}

Note that postman.setNextRequest only establishes the next request in the workflow. It does not actually execute the request. So we can call the function in the Pre-request tab or the Test tab.

Running the Collection

You can run this collection the same way you would run any Postman collection, either through the Collection Runner or with a Newman command. The result will be not two, but 21 completed requests, one for the initial list and then one for each of Africa’s 20 time zones. And if that number changes, our dynamically iterating collection will be ready!

loop collection runner

The complete collection for this example is available on my Github. The postman.setNextRequest function is also useful for if/then logic within your collection, where a one request can be followed by different requests depending on the request response. Hopefully this little tip will inspire you to see what else you can do with the Postman API!

4 thoughts on “Postman How-to: Dynamic Iteration Within a Collection

Leave a comment