{"info":{"_postman_id":"1dacb288-d11b-4eb4-9942-7f0e23216b7a","name":"DynamicLoop","description":"<html><head></head><body><p>Collection demonstrating how to use postman.setNextRequest to do dynamic iteration within a collection based on the HTTP response without using CSV files.</p>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[],"owner":"961600","collectionId":"1dacb288-d11b-4eb4-9942-7f0e23216b7a","publishedId":"RztkNV1m","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"EF5B25"},"publishDate":"2019-01-31T00:52:49.000Z"},"item":[{"name":"Get Time Zones","event":[{"listen":"test","script":{"id":"e3d7538a-845b-4a62-a64e-77ea70ef20ad","exec":["","pm.test(\"Status code is 200\", function () {","    pm.response.to.have.status(200);","});","","var tz = pm.response.json();","","// The worldtimeapi is rate-limited, so filter just African ","// time zones to make the number of requests manageable ","var zones = []","for (var z in tz){","    var zone = tz[z]","    if (zone.startsWith(\"Africa\")){","        zones.push(zone)","    }","}","console.log(zones)","// now set our collection variables with the array and the initial index (0)","pm.variables.set(\"timezones\", JSON.stringify(zones))","pm.variables.set(\"tz_index\", 0)"],"type":"text/javascript"}}],"id":"334879f6-ffdb-4267-b53b-e8c3d5ea59d4","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://worldtimeapi.org/api/timezone","description":"<p>Initial request to fetch all the time zones and save the subset of African timezones</p>\n","urlObject":{"protocol":"http","path":["api","timezone"],"host":["worldtimeapi","org"],"query":[],"variable":[]}},"response":[],"_postman_id":"334879f6-ffdb-4267-b53b-e8c3d5ea59d4"},{"name":"Get Time For Zone","event":[{"listen":"prerequest","script":{"id":"83eae302-b6e4-4df5-a22d-ad8e34903c35","exec":["// 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])","","// If we are *not* on the last item in the list,","// increment the index and set the next request to ","// this one. Otherwise end the loop.","","// Note that we can do this in the pre-request","// because setNextRequest doesn't execute anything,","// it just establishes workflow","if (index + 1 < tz.length){","    pm.variables.set('tz_index', index + 1)","    postman.setNextRequest('Get Time For Zone')","}else{","    postman.setNextRequest(null)","}"],"type":"text/javascript"}},{"listen":"test","script":{"id":"931b7f05-bf7f-46be-b154-87f2ca562ec9","exec":["","pm.test(\"Status code is 200\", function () {","    pm.response.to.have.status(200);","});","","var zone = pm.variables.get(\"tz\")","pm.test(\"Timezone \" + zone + \" is returned correctly\", function () {","    var jsonData = pm.response.json();","    pm.expect(jsonData.timezone).to.eql(zone);","});"],"type":"text/javascript"}}],"id":"1b7062e7-0714-422e-89e1-44ca1d468169","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[],"url":"http://worldtimeapi.org/api/timezone/{{tz}}","description":"<p>Request which will be called for each time zone</p>\n","urlObject":{"protocol":"http","path":["api","timezone","{{tz}}"],"host":["worldtimeapi","org"],"query":[],"variable":[]}},"response":[],"_postman_id":"1b7062e7-0714-422e-89e1-44ca1d468169"}],"event":[{"listen":"prerequest","script":{"id":"3127a935-642b-4baa-a257-8dfcf7fd6e03","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"cd89fb0d-701e-421e-9526-cc7164f4e964","type":"text/javascript","exec":[""]}}]}