Running a request with multiple values in body

hi colleagues,

there are several similar questions to mine in community,but i still dont get the answer which i need.
I have two api calls:

  • Call 1, for getting some information
  • Call 2 for deleting/cleaning up some information based on call 1

Call 1 returns a list like this

{
    "childNodeList": [
        {
            "tenantId": "*",
            "retailStoreId": "*",
            "workstationId": "*",
            "structureUniqueName": "A",
            "systemName": "*",
            "structureName": "*",
            "activeVersion": "*",
            "country": "*",
            "countryShort": "*"
        },
        {
            "tenantId": "*",
            "retailStoreId": "*",
            "structureUniqueName": "B",
            "systemName": "*",
            "structureName": "*",
            "country": "*",
            "countryShort": "*"
        },
        {
            "tenantId": "*",
            "retailStoreId": "*",
            "structureUniqueName": "C",
            "systemName": "*",
            "structureName": "* *",
            "country": "*",
            "countryShort": "*"
        },
        {
            "tenantId": "*",
            "retailStoreId": "*",
            "structureUniqueName": "D",
            "systemName": "*",
            "structureName": "*",
            "country": "*",
            "countryShort": "*"
        },
        {
            "tenantId": "*",
            "retailStoreId": "*",
            "structureUniqueName": "E",
            "systemName": "*",
            "structureName": "*",
            "country": "*",
            "countryShort": "*"
        }
    ]
}

Call 2 must delete all objects of this list.Request body of Call 2 looks like:

{
  "node": {
    "tenantId": "*",
    "retailStoreId": "*",
    "workstationId": "*",
    "structureUniqueName": "{{some variable}}", --> must be value A,B,C..
    "systemName": ""
  },
  "user": "1001"
}

So what i want is to repeat the Call2 for all values from list(A,B,C,D…), {{some variable} must resolve fisrt the value A and delete this object,then value B and so on.

What i did

  1. I created an array collection variable structureUniqueName which contains all mentioned values (A,B,C…).I did this in Test tab Call 1
//get structureUniqueName for each c4r node in SM
responseJson = JSON.parse(responseBody);
var array=[];
var list = (responseJson.childNodeList).length
console.log(list);
   
for (var i = 0; i < list; i++) 
    {
    var counter = responseJson.childNodeList[i];
    structureUniqueName=counter.structureUniqueName
    array.push(structureUniqueName)
    }
    array= JSON.stringify(array)
    pm.collectionVariables.set("structureUniqueName", array);
  1. In Pre-request Script Call 2,i used the JS function shift() with the hope that it will read the first element and remove it from list,then the Call2 will be executed with first element of list (A) and then (B) and so on.
var uni = pm.collectionVariables.get("structureUniqueName");
console.log(uni);
var currentuni = uni.shift();
console.log(currentuni);

For my understating the variable uni must contain all values(A,B,C…) and currentuni must contain only the first element,but the result is that both of them have same values,it looks like the shift() does not work at all.That leads to the problem that Call 2 fails because in the request body:

{
  "node": {
    "tenantId": "*",
    "retailStoreId": "*",
    "workstationId": "*",
    "structureUniqueName": "{{currentuni}}",
    "systemName": ""
  },
  "user": "1001"
}

{{currentuni}} will be resolved with worng value.If i call the Call2 with body

{
  "node": {
    "tenantId": "*",
    "retailStoreId": "*",
    "workstationId": "*",
    "structureUniqueName": "A",
    "systemName": ""
  },
  "user": "1001"
}

or

{
  "node": {
    "tenantId": "*",
    "retailStoreId": "*",
    "workstationId": "*",
    "structureUniqueName": "B",
    "systemName": ""
  },
  "user": "1001"
}

it works like a charm.
Does anyone maybe have a better solution?

Hi @oalimerko,

The reason shift is not working is because your collection variable is a string.

Here is how i fixed the issue.

//Saving the list as a comma separated string.
responseJson = JSON.parse(responseBody);
var array=[];
let structureUniqueName = '';
   
responseJson.data.childNodeList.forEach(item => {
    structureUniqueName+=item.structureUniqueName + ','
});

// Slice here is to remove the trailing ","
pm.collectionVariables.set("structureUniqueName", structureUniqueName.slice(0,-1));

And then your second request will pull the data like this.

// Here storing the comma separated list as array and now shift works as expected. 
var uni = [...pm.collectionVariables.get("structureUniqueName")];
console.log(uni);
var currentuni = uni.shift();
console.log(currentuni);
1 Like

hi @archerZ ,

in my code the issue was that i was stringifying the collection variable array= JSON.stringify(array).In order to push the objects to array i used this array.push(${structureUniqueName})

Here the whole code

var array=[];
var list = (responseJson.childNodeList).length ;
//console.log(list);
for (var i = 0; i < list; i++)
{
    var counter = responseJson.childNodeList[i];
    structureUniqueName=counter.structureUniqueName
    array.push(`${structureUniqueName}`)
    }
    //array= JSON.stringify(array)
    pm.collectionVariables.set("structureUniqueName", array);
    console.log(array);
}
pm.test("Status code is 200", function () {
  pm.response.to.have.status(200);
});