{"info":{"_postman_id":"efaa4a37-031f-4c74-9663-02471225dcd3","name":"Multi-request loop","description":"This Collection gets a list of IDs, then loops through 3 POST requests that submit these IDs until there are none left.\n\nThe layout is as follows:\n\n\n**GET ID list:** https://postman-echo.com/get\n\nDescription:\n- response contains an array of numbers to use as dummy data.\n\n\nTest script:\n- Parse IDs out of JSON, and store them as an array\n- `JSON.stringify()` the array and store in an environment using `pm.environment.set('key',value)`;\n- Store the length of the array in the environment\n\n**1st POST request:** https://postman-echo.com/post?test1={{id}}\n\nDescription:\n- A request meant to do something with the IDs we get in the **GET ID list** request.\n- Also contains a pre-request script that is critical for looping through the data above\n\n\nPre-request script:\n- Check for/declare a counter variable in the environment using `pm.environment.has('key')`, and `pm.environment.set('key',value)` (this is so you can iterate through the array you stored when looping requests)\n- Parse the array stored in the environment, and set value for the `{{id}}` variable based on the index of the array you're iterating through\n- Increment your counter, and update the environment variable with the new value (`pm.environment.set('key',value);`)\n\n**2nd POST request:** https://postman-echo.com/post?test2={{id}}\n\nDescription:\n- A request meant to do something with the IDs we get in the **GET ID list** request.\n\n\n**3rd POST request:** https://postman-echo.com/post?test3={{id}}\n\nDescription:\n- A request meant to do something with the IDs we get in the **GET ID list** request.\n- Also contains a test script, which facilitates the looping of the 3 get requests in this Collection.\n\nTest script:\n- Check the length of the parsed array against your counter variable, and if there are more indexes use `postman.setNextRequest('request_name')` to loop back to the 2nd request in the Collection.","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json"},"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":"Description:\n- response contains an array of numbers to use as dummy data.\n\n\nTest script:\n- Parse IDs out of JSON, and store them as an array\n- `JSON.stringify()` the array and store in an environment using `pm.environment.set('key',value)`;\n- Store the length of the array in the environment"},"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":{"raw":"https://postman-echo.com/post?test2={{id}}","protocol":"https","host":["postman-echo","com"],"path":["post"],"query":[{"key":"test2","value":"{{id}}"}]},"description":"Description:\n- A request meant to do something with the IDs we get in the **GET ID list** request.\n- Also contains a pre-request script that is critical for looping through the data above\n\n\nPre-request script:\n- Check for/declare a counter variable in the environment using `pm.environment.has('key')`, and `pm.environment.set('key',value)` (this is so you can iterate through the array you stored when looping requests)\n- Parse the array stored in the environment, and set a value for the `{{id}}` variable based on the index of the array you're iterating through\n- Increment your counter, and update the environment variable with the new value (`pm.environment.set('key',value);`)"},"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":{"raw":"https://postman-echo.com/post?test2={{id}}","protocol":"https","host":["postman-echo","com"],"path":["post"],"query":[{"key":"test2","value":"{{id}}"}]},"description":"Description:\n- A request meant to do something with the IDs we get in the **GET ID list** request."},"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":{"raw":"https://postman-echo.com/post?test3={{id}}","protocol":"https","host":["postman-echo","com"],"path":["post"],"query":[{"key":"test3","value":"{{id}}"}]},"description":"Description:\n- A request meant to do something with the IDs we get in the **GET ID list** request.\n- Also contains a test script, which facilitates the looping of the 3 get requests in this Collection.\n\nTest script:\n- Check the length of the parsed array against your counter variable, and if there are more indexes use `postman.setNextRequest('request_name')` to loop back to the 2nd request in the Collection."},"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":[""]}}]}