{"info":{"_postman_id":"35e8c264-4fcb-4216-b797-cb80aca63624","name":"Storing response data as globals, and POSTing using a loop","description":"<html><head></head><body><h1 id=\"about-this-collection\">About this Collection</h1>\n<p>This Collection is an example of setting global variables based on an array in response data, then using looping to POST each value.</p>\n</body></html>","schema":"https://schema.getpostman.com/json/collection/v2.0.0/collection.json","toc":[{"content":"About this Collection","slug":"about-this-collection"}],"owner":"5293839","collectionId":"35e8c264-4fcb-4216-b797-cb80aca63624","publishedId":"SW7XbVew","public":true,"customColor":{"top-bar":"FFFFFF","right-sidebar":"303030","highlight":"EF5B25"},"publishDate":"2019-11-18T23:37:27.000Z"},"item":[{"name":"Get Array","event":[{"listen":"test","script":{"id":"61370787-a69f-4b9d-9039-d509ca3ef990","exec":["var array = pm.response.json().args.parentProperty[0].childProperty2; // the array we're iterating through, stored in childProperty2[]","","for(var i = 0; i < array.length-1; i++) // loop through the array","{","    var pieceValue = array[i].id; // the specific value were setting as a global variable","    pm.globals.set('pieceId'+i,pieceValue); // use i to increment the name of the global variable we're setting, and set it to the appropriate value from childProperty2[]","}"],"type":"text/javascript"}},{"listen":"prerequest","script":{"id":"a8e41860-e2ba-4b8b-9f34-11ffc22cbf4a","exec":[""],"type":"text/javascript"}}],"id":"2c9dd426-8146-43e3-9f75-002c80c76523","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"GET","header":[{"key":"Content-Type","name":"Content-Type","type":"text","value":"application/json"}],"body":{"mode":"raw","raw":"{\n\t\"parentProperty\":[\n\t\t{\n\t\t\"childProperty1\":[\n\t\t\t\"a\",\"b\",\"c\"],\n\t\t\"childProperty2\":[\n\t\t\t{\"id\":1},\n\t\t\t{\"id\":2},\n\t\t\t{\"id\":3},\n\t\t\t{\"id\":4},\n\t\t\t{\"id\":5},\n\t\t\t{\"id\":6},\n\t\t\t{\"id\":7},\n\t\t\t{\"id\":8},\n\t\t\t{\"id\":9},\n\t\t\t{\"id\":0}\n\t\t\t]\n\t\t}\n\t\t]\n}"},"url":"https://postman-echo.com/get","description":"<p>This request uses Postman Echo to get a response with sample data for us to use. The request send a JSON body, which the service echos back.</p>\n<p>The important part of this request is its Test Script, which takes data from an array within the response and stores it as global variables.</p>\n","urlObject":{"protocol":"https","path":["get"],"host":["postman-echo","com"],"query":[],"variable":[]}},"response":[],"_postman_id":"2c9dd426-8146-43e3-9f75-002c80c76523"},{"name":"POST Array Data","event":[{"listen":"prerequest","script":{"id":"8843a526-c311-4d13-8074-d94406162665","exec":["// check to see if there is a counter run-scoped variable, and if there isn't create one with value=0.","// this is for iterating through the global variables stored, and stopping when there's none left.","if(!pm.variables.has('counter')){","    pm.variables.set('counter',0);","}","","// get the current value of the run-scoped variable \"counter\"","var counter = pm.variables.get('counter');","","// if there is a variable called (e.g.) pieceId0, get it's value and set a run-scoped variable","if(pm.globals.has(\"pieceId\" + counter)){","    var pieceIdValue = pm.globals.get(\"pieceId\" + counter)","    pm.variables.set(\"pieceIdDynamic\",pieceIdValue)","}","","// increment the counter variable","counter++;","","// store the current value of the Sandbox variable counter, in the run-scoped variable \"counter\"","// this is so that the counter value persists between loops of the POST request","pm.variables.set('counter',counter);","","// if there is another global variable to send, set the next request as this same request (loop it again)","if(pm.globals.has(\"pieceId\"+counter)){","    postman.setNextRequest(\"POST Array Data\")","}"],"type":"text/javascript"}},{"listen":"test","script":{"id":"a4b15c06-e252-47a9-89df-ca44c65220fd","exec":["pm.test(\"content-type is application/json\", function () {","    let responseHeaders = pm.response.headers.all();","    let index = -1;","    for(var i = 0; i < responseHeaders.length-1; i++)","    {","        console.log(responseHeaders[i]);","        if(responseHeaders[i].key === 'Content-Type')","        {","            index = i;","            break;","        }","    }","    ","    pm.expect(index).to.not.equal(-1);","    pm.expect(responseHeaders[index].value).to.equal('application/json; charset=utf-8');","});"],"type":"text/javascript"}}],"id":"08f8138d-0962-4e7e-bbdd-7b6231610665","protocolProfileBehavior":{"disableBodyPruning":true},"request":{"method":"POST","header":[{"key":"Content-Type","value":"application/json; charset=utf-8","type":"text"}],"body":{"mode":"raw","raw":""},"url":"https://postman-echo.com/post?pieceId={{pieceIdDynamic}}","description":"<p>This request POSTs data stored in global variables, using postman.setNextRequest() in a pre-request script to loop the request over and over until all data has been sent.</p>\n","urlObject":{"protocol":"https","path":["post"],"host":["postman-echo","com"],"query":[{"key":"pieceId","value":"{{pieceIdDynamic}}"}],"variable":[]}},"response":[],"_postman_id":"08f8138d-0962-4e7e-bbdd-7b6231610665"}],"event":[{"listen":"prerequest","script":{"id":"9e2f864a-7603-418e-b7dd-abe6da3ddbc8","type":"text/javascript","exec":[""]}},{"listen":"test","script":{"id":"c3325dff-9b73-4597-adc3-c89449f814a0","type":"text/javascript","exec":[""]}}]}