I'd like to loop through a Postman Collection of 3 API Requests until the JSON Array is complete.
For example, I have a Collection of 3 Requests (below) called Compass:
Compass (Collection Name)
POST Create Submission
GET Retrieve Submission
DEL Delete Submission
In my Collection called Compass, I have a Pre-Request Script that sends a Request for 10 UUID's (below). I'm able to store the 10 UUID's from the Response into a JSON Array in hopes to later iterate through the array via a Looping the collection until the array is complete. The UUIDs are saved as a Environment Variable called 'ids'.
Pre-Request Script:
pm.sendRequest({
url: "https://www.uuidgenerator.net/api/version1/10",
method: 'GET',
}),
pm.test("UUIDs"), function() {
let response = pm.response.text().split('\n'), jsonObject = {}
response.forEach((item,index)=>{
jsonObject[`UUID_${index}`] = item });
console.log(jsonObject)
pm.environment.set("ids", jsonObject)
};
My goal is to iterate 1 UUID at a time through the collection. So, the Same UUID goes through the Create Submission, Receive Submission and Delete Submission, and once that completes, the next UUID in the array will loop, Once the array of 10 UUID's is done, it will stop the Collection Loop.
The body of the Requests has a bunch of data but ultimately I just have put the ID variable in the value spot of the "id" key, like so: (Quotes because it must be a String)
"id":"{{ids}}",
Also in the First Request (Create Submission) I have a Pre-request that sends to the following Request (Receive Submission) if the length of the env variable is create than 0.
pm.environment.get("ids");
if (ids.length < 0){
postman.setNextRequest("Create Submissions");
} else {
pm.setNextRequest(null)
}
I've also plugged this into the Test field to see what populates:
var uuids = pm.environment.get("ids")
console.log(uuids)
And in the Response I'm receiving a null ?
POST https://XXXXX/submissions
null
It doesn't seem the environment variable is being called from the Pre-request script.
I'm sure I'm iterating through this incorrectly and probably need to figure out a better function that goes through the Array one by one. I also wonder if I'm putting these Scripts in the wrong places.