Use a list environment variable within pre-request scriipt

Would anyone know how to import a list (of IPv4 addresses in this instance) into a postman Pre-request script? So for example - I have a request that is working, but uses a ā€˜hard-codedā€™ list of IPs in the Pre-request script - what Iā€™m after idsa way to import this list somehow, maybe an environment variable or an imported list:

var deployments = [];
// Iterate over the range for i (1-4) number of nodes
var nodeIP= ["10.0.1.1","10.0.1.2","10.0.1.3","10.0.2.1","10.0.2.2" ]
for (var x = 0; x < nodeIP.length; x++) {
    // Iterate over the range for i (1-30) for the number of deployment
                ;
        // Create deployment object
        var deployment = {
            "template-name": "SRL_ACL_System_Filter",
            "target-data": JSON.stringify(pstmtsrlpayload),
            "targets": [
                {
                    "target": "/nsp-equipment:network/network-element[ne-id='"+nodeIP[x]+"']",
                    "target-identifier-value": "system-filter"
                }
            ],
            "deployment-action": "deploy",
            "merge": false
        };

I have tried things like the following without success:

var nodeIP = pm.environment.get("pdiSrlList1");

where the environment variable is:

pdiSrlList1 = "10.0.1.1","10.0.1.2","10.0.1.3","10.0.2.1","10.0.2.2"

HI @daiwattnokia. Welcome to the Postman Community :postman_logo:

What will be the data source of the list of IPs? Are you looking to get them from an API, have them in a csv or JSON file, etc? Itā€™ll be helpful to know how this data will be presented to suggest a good way of programmatically ā€œimportingā€ it.

Thanks @gbadebo-bello,
I donā€™t mind how they are presentecd - CSV, JSON, postman environment variable or something else - that is easy to adapt. So letā€™s say CSV - thatā€™s the simplest - I have to present this as an easy method to a field community, who wonā€™t want any excessive complexity

Hi @daiwattnokia.

What errors did you get when you used an environment variable? Can you share maybe a sample code of how youā€™re using this with an environment variable. Ideally, that should work for your usecase.

You need to store the variable as an array, and then JSON parse it otherwise it will be treated as a string.

image

var nodeIP =JSON.parse(pm.environment.get("nodeIP"));

for (var x = 0; x < nodeIP.length; x++) {
    console.log(nodeIP[x]);
}

image

If you want to use the collection runner and JSON as the input file, then the JSON file should look like the following.

[
    {
        "nodeIP": [
            "10.0.1.1",
            "10.0.1.2",
            "10.0.1.3",
            "10.0.2.1",
            "10.0.2.2"
        ]
    }
]

You donā€™t need to parse the JSON using this method.

var nodeIP = pm.iterationData.get("nodeIP");

for (var x = 0; x < nodeIP.length; x++) {
    console.log(nodeIP[x]);
}

Thanks will try those suggestion today and get back to you

Thanks Mike, that worked perfectly :grinning:

Did you really mark your own post as the solution? Mmmm.

Sorry, my mistake, Iā€™ll correct that

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.