I’m getting into automation and API calls to help deploy cybersecurity environments faster. For 1 project I’m working on I need to create 778 DNS FQDN objects in a firewall. I’ve setup postman to handle this, created a collection, created a CSV file and tested the collection manually.
I can run the collection fine 1 by 1. Now I want to pass the variable values from the CSV file to only 1 POST request in the collection and not itterate over everything in the collection 778 times.
The “Create Firewall address” needs to itterate over the CSV file while the others need to run only once. The login script returns the session ID I can pass to the others but I don’t need to log in 778 times offcourse.
I’ve been going through the postman documentation and this forum but am a little lost as I’m to much of a noob atm to fully grasp everything.
The only way I can think of doing this is to have your 778 objects as a single JSON string in your CSV file.
You can then import this into a single variable.
This needs to be parsed, and converted into an array.
At which point you can craft a forEach loop using sendRequest() in the Tests tab for the Lock Workspace. This means that “Create firewall address” won’t be a separate request.
It needs to be an array, as forEach won’t work with a JavaScript object which is initially created when you parse the data.
Have a look at the following link, as there is a useful function for working with arrays that will probably benefit you.
//return an array of objects according to key, value, or key and value matching
function getObjects(obj, key, val) {
var objects = [];
for (var i in obj) {
if (!obj.hasOwnProperty(i)) continue;
if (typeof obj[i] == 'object') {
objects = objects.concat(getObjects(obj[i], key, val));
} else
//if key matches and value matches or if key matches and value is not passed (eliminating the case where key matches but passed value does not)
if (i == key && obj[i] == val || i == key && val == '') { //
objects.push(obj);
} else if (obj[i] == val && key == ''){
//only add if the object is not already in the array
if (objects.lastIndexOf(obj) == -1){
objects.push(obj);
}
}
}
return objects;
}
Have a look at the following for an example of using the function with an array.