Post Looping through an Array not working

I’ve followed the example but I’m unable to get this pre-request script to loop through my array. It’s only posting the first item.

Body
{
“carrierpkey”:“{{clientpkey}}”
}

Pre-requestScript
var pkeys = pm.environment.get(“pkeys”);

if(!pkeys){
pkeys =[ 31922, 456789, 75225];
}

var currentpkey = pkeys.shift();
pm.environment.set(“clientpkey”,currentpkey);
pm.environment.set(“pkeys”, pkeys);

Tests
var pkeys = pm.environment.get(“pkeys”);

if(pkeys && pkeys.length > 0){
postman.setNextRequest(“My URL Here”)

} else{
postman.setNextRequest(null);
}

I figured it out. For the “run” process I need to add the iterations amount. This doesn’t seem correct, why doesn’t it just loop based on the array length?

I can’t see a loop here. I can see a couple of IF statements.

What do you mean by the “run” process. Is this set to loop?

Do you mean the shift line which should just remove and return the first element of the array.

Have a look at this link which has an example of a for and forEach loop if that is what you need.

Isn’t this the loop? Get the length of the array, setnextrequest until the length goes to zero

if(pkeys && pkeys.length > 0){
postman.setNextRequest(“My URL Here”)

} else{
postman.setNextRequest(null);
}

I’m using this sample but not sure what I’m missing
https://www.youtube.com/watch?v=sZKoBQ1HpKw

Is that first setNextRequest basically the same request looping back on itself.

When you say Tests, is this in the Tests tab on the request so it runs after the request.

I sort of get the logic. No idea if it actually works. I don’t have time today but may try and replicate this tomorrow as I can see where this could be useful.

This is in the “test” tab. And yes, the setNextRequest should iterate through the length of the array and post accordingly.

@otrjgn

I finally got round to testing this and it does work with some slight modifications.

I didn’t need to set the iterations to 3 in the run options.

This is the pre-request script.

var array = pm.collectionVariables.get("pkeys");

if(array.length === 0){
    array = [31922, 456789, 75225];
}

pm.test("pre-req array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty; 
});

// console.log("pre-req array: " + array);

var currentpkey = array.shift();
// console.log("pre-req current key: " + currentpkey);
// console.log("pre-req updated array " + array);

pm.collectionVariables.set("clientpkey",currentpkey);
pm.collectionVariables.set("pkeys", array);

This is the tests tab script.

var array = pm.collectionVariables.get("pkeys");

var expectedResponse = pm.collectionVariables.get("clientpkey");
var actualResponse = parseInt(pm.response.json().args.test);

pm.test(`tests-tab clientpkey = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
postman.setNextRequest("Request1");

} else {
postman.setNextRequest(null);
}

I’m just using the Postman echo URL for the test including the clientpkey variable which it will echo back to you. I’ve included an assertion in the tests tab that check the response included the key that was sent.

https://postman-echo.com/get?test={{clientpkey}

image

You can see in the results that it ran three times, but in a single iteration.

I can see some uses for this approach, where you want a particular request to loop through data from a previous request.

@michaelderekjones Thanks for the help. All of a sudden I’ve started getting this error and now nothing works. Any idea on why this is happening?

ProcessEntityChange failed: Could not convert string to integer: null. Path ‘carrierpkey’, line 2, position 22.

I noticed this a while back.

Something changed, and you need to stringify when storing the array, and parse it when retrieving it.

Not sure when it changed though as this was working at one point.

Pre-request script.

var array = JSON.parse(pm.collectionVariables.get("pkeys"));

if(array.length === 0){
    array = [31922, 456789, 75225];
}

pm.test("pre-req array is not empty", () => {
    pm.expect(array).to.be.an("array").that.is.not.empty; 
});

// console.log("pre-req array: " + array);

var currentpkey = array.shift();
// console.log("pre-req current key: " + currentpkey);
// console.log("pre-req updated array " + array);

pm.collectionVariables.set("clientpkey",currentpkey);
pm.collectionVariables.set("pkeys", JSON.stringify(array));

Tests tab

var array = JSON.parse(pm.collectionVariables.get("pkeys"));

var expectedResponse = pm.collectionVariables.get("clientpkey");
var actualResponse = pm.response.json().args.test;

pm.test(`tests-tab clientpkey = ${expectedResponse}`, () => {
    pm.expect(actualResponse).to.eql(expectedResponse); 
});

if (array.length > 0){
    postman.setNextRequest("Request1");

    } else {
        postman.setNextRequest(null);
    }

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