{"info":{"_postman_id":"efaa4a37-031f-4c74-9663-02471225dcd3","name":"Multi-request loop","description":"<html><head></head><body><p>This Collection gets a list of IDs, then loops through 3 POST requests that submit these IDs until there are none left.</p>\n<p>The layout is as follows:</p>\n<p><strong>GET ID list:</strong> <a href=\"https://postman-echo.com/get\">https://postman-echo.com/get</a></p>\n<p>Description:</p>\n<ul>\n<li>response contains an array of numbers to use as dummy data.</li>\n</ul>\n<p>Test script:</p>\n<ul>\n<li>Parse IDs out of JSON, and store them as an array</li>\n<li><code>JSON.stringify()</code> the array and store in an environment using <code>pm.environment.set('key',value)</code>;</li>\n<li>Store the length of the array in the environment</li>\n</ul>\n<p><strong>1st POST request:</strong> <a href=\"https://postman-echo.com/post?test1=%7B%7Bid%7D%7D\">https://postman-echo.com/post?test1={{id}}</a></p>\n<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n<li>Also contains a pre-request script that is critical for looping through the data above</li>\n</ul>\n<p>Pre-request script:</p>\n<ul>\n<li>Check for/declare a counter variable in the environment using <code>pm.environment.has('key')</code>, and <code>pm.environment.set('key',value)</code> (this is so you can iterate through the array you stored when looping requests)</li>\n<li>Parse the array stored in the environment, and set value for the <code>{{id}}</code> variable based on the index of the array you're iterating through</li>\n<li>Increment your counter, and update the environment variable with the new value (<code>pm.environment.set('key',value);</code>)</li>\n</ul>\n<p><strong>2nd POST request:</strong> <a href=\"https://postman-echo.com/post?test2=%7B%7Bid%7D%7D\">https://postman-echo.com/post?test2={{id}}</a></p>\n<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n</ul>\n<p><strong>3rd POST request:</strong> <a href=\"https://postman-echo.com/post?test3=%7B%7Bid%7D%7D\">https://postman-echo.com/post?test3={{id}}</a></p>\n<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n<li>Also contains a test script, which facilitates the looping of the 3 get requests in this Collection.</li>\n</ul>\n<p>Test script:</p>\n<ul>\n<li>Check the length of the parsed array against your counter variable, and if there are more indexes use <code>postman.setNextRequest('request_name')</code> to loop back to the 2nd request in the Collection.</li>\n</ul>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[],"owner":"5293839","collectionId":"efaa4a37-031f-4c74-9663-02471225dcd3","publishedId":"SWLiY5ab","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"EF5B25"},"publishDate":"2020-01-14T19:43:36.000Z"},"item":[{"name":"Get ID list","event":[{"listen":"test","script":{"id":"c78ebee0-0c48-4658-9f99-d78c237b5a0e","exec":["// Get array from reponse","var idArray = pm.response.json().args.idArray;","","// Get the length of the array","var idArrayLength = idArray.length;","","// Store the array length in the environment","pm.environment.set('idArrayLength',idArrayLength);","// Store the stringified idArray in the environment","pm.environment.set('idArray',JSON.stringify(idArray));"],"type":"text/javascript"}}],"id":"540122cc-d3bb-49cb-94d0-b8d8e370ce39","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"Content-Type","name":"Content-Type","value":"application/json","type":"text"}],"body":{"mode":"raw","raw":"{\n\t\"idArray\":[1,2,3,4,5,6,7,8,9,0]\n}"},"url":"https://postman-echo.com/get","description":"<p>Description:</p>\n<ul>\n<li>response contains an array of numbers to use as dummy data.</li>\n</ul>\n<p>Test script:</p>\n<ul>\n<li>Parse IDs out of JSON, and store them as an array</li>\n<li><code>JSON.stringify()</code> the array and store in an environment using <code>pm.environment.set('key',value)</code>;</li>\n<li>Store the length of the array in the environment</li>\n</ul>\n","urlObject":{"protocol":"https","path":["get"],"host":["postman-echo","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"540122cc-d3bb-49cb-94d0-b8d8e370ce39"},{"name":"1st POST request","event":[{"listen":"prerequest","script":{"id":"3e11e9ae-865e-455b-96c4-e7fc40f0cd8b","exec":["//check to see if a counter variable has been declared, and if so ","if(!pm.environment.has('counter'))","{","    pm.environment.set('counter',0);","}","","//get the counter value","var counter = pm.environment.get('counter');","","//parse the array we stored in the 1st request, since it gets stored as a string","var idArray = JSON.parse(pm.environment.get('idArray'));","","//get the value of the idArray at the current iteration (counter)","var id = idArray[counter];","","//store an environment variable with the value of the current id","//the {{id}} variable in the URL of this request will end up resolving to this value","","pm.environment.set('id',id);","","//incremement the counter, and store it in the environment","counter++;","pm.environment.set('counter',counter);"],"type":"text/javascript"}},{"listen":"test","script":{"id":"8917ad00-3459-40df-9490-5a4e4185d669","exec":[""],"type":"text/javascript"}}],"id":"1bd9fc30-23f1-4c90-aefc-5fe12a72b2d5","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[],"url":"https://postman-echo.com/post?test2={{id}}","description":"<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n<li>Also contains a pre-request script that is critical for looping through the data above</li>\n</ul>\n<p>Pre-request script:</p>\n<ul>\n<li>Check for/declare a counter variable in the environment using <code>pm.environment.has('key')</code>, and <code>pm.environment.set('key',value)</code> (this is so you can iterate through the array you stored when looping requests)</li>\n<li>Parse the array stored in the environment, and set a value for the <code>{{id}}</code> variable based on the index of the array you're iterating through</li>\n<li>Increment your counter, and update the environment variable with the new value (<code>pm.environment.set('key',value);</code>)</li>\n</ul>\n","urlObject":{"protocol":"https","path":["post"],"host":["postman-echo","com"],"query":[{"key":"test2","value":"{{id}}"}],"variable":[]}},"response":[],"_postman_id":"1bd9fc30-23f1-4c90-aefc-5fe12a72b2d5"},{"name":"2nd POST request","event":[{"listen":"prerequest","script":{"id":"3e11e9ae-865e-455b-96c4-e7fc40f0cd8b","exec":[""],"type":"text/javascript"}},{"listen":"test","script":{"id":"8917ad00-3459-40df-9490-5a4e4185d669","exec":[""],"type":"text/javascript"}}],"id":"99abf49f-cc84-46ef-b495-070a1d05881e","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[],"url":"https://postman-echo.com/post?test2={{id}}","description":"<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n</ul>\n","urlObject":{"protocol":"https","path":["post"],"host":["postman-echo","com"],"query":[{"key":"test2","value":"{{id}}"}],"variable":[]}},"response":[],"_postman_id":"99abf49f-cc84-46ef-b495-070a1d05881e"},{"name":"3rd POST request","event":[{"listen":"prerequest","script":{"id":"3e11e9ae-865e-455b-96c4-e7fc40f0cd8b","exec":[""],"type":"text/javascript"}},{"listen":"test","script":{"id":"8917ad00-3459-40df-9490-5a4e4185d669","exec":["//get the counter value and array length","var counter = pm.environment.get('counter');","var arrayLength = pm.environment.get(\"idArrayLength\");","","//check to see if there are more indexes in the array to go through, and if so ","if (counter < arrayLength)","{","    postman.setNextRequest('1st POST request');","}","else","{","    postman.setNextRequest(null);","}"],"type":"text/javascript"}}],"id":"f17c28b3-5097-47a2-bce5-91611b17c06e","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[],"url":"https://postman-echo.com/post?test3={{id}}","description":"<p>Description:</p>\n<ul>\n<li>A request meant to do something with the IDs we get in the <strong>GET ID list</strong> request.</li>\n<li>Also contains a test script, which facilitates the looping of the 3 get requests in this Collection.</li>\n</ul>\n<p>Test script:</p>\n<ul>\n<li>Check the length of the parsed array against your counter variable, and if there are more indexes use <code>postman.setNextRequest('request_name')</code> to loop back to the 2nd request in the Collection.</li>\n</ul>\n","urlObject":{"protocol":"https","path":["post"],"host":["postman-echo","com"],"query":[{"key":"test3","value":"{{id}}"}],"variable":[]}},"response":[],"_postman_id":"f17c28b3-5097-47a2-bce5-91611b17c06e"}],"event":[{"listen":"prerequest","script":{"id":"1e4c04ba-e517-4280-b78e-4e3dd05347b8","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"a31f0dd5-2fbc-4d5c-bb87-81733b853207","type":"text/javascript","exec":[""]}}]}