Postman delete multiple resource IDs

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question: POSTMAN: How to delete multiple Resource IDs in one go?

Details (like screenshots):
I have a list of 30 resources IDs and would like to use {{url}}/resource_delete on Postman adding this list of IDs

How I found the problem:
N/A

I’ve already tried:
{
“id”: “{{id}}”
}

let resources = pm.collectionVariables.get(“resources”);

if(!resources || resources.length == 0)

{

resources = [“ba08971c-83de-777c-beb2-2dbe91e727e6”,

“c6ec9412-e19e-999-bcc8-0a9900f3c0ba”,

“268e8bd9-3381-0000-81d3-38fd4fa11110”

];

}

let currentResources = resources.shift();

pm.collectionVariables.set(“id”, currentResources);

pm.collectionVariables.set(“resources”, resources);

const resources = pm.collectionVariables.get(“resources”);

if (resources && resources.length > 0){
postman.setNextRequest(“multiple_resources_delete_inonego”);
} else {
postman.setNextRequest(null);
}

pm.test(“Status code is 200”, function () {
pm.response.to.have.status(200);
});

But it doesnt loop. I have to run (pushing the button SEND) 3 times to delete these 3 resources

The following should work. (This is running against postman echo and I’ve just put the {{id}} into the URL instead of the body.

image

Pre-request script.

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

if(array.length === 0){
    array = [
        "ba08971c-83de-777c-beb2-2dbe91e727e6",
        "c6ec9412-e19e-999-bcc8-0a9900f3c0ba",
        "268e8bd9-3381-0000-81d3-38fd4fa11110"
    ];
}

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

var currentResource = array.shift();

pm.collectionVariables.set("id",currentResource);
pm.collectionVariables.set("resources", JSON.stringify(array));

Tests tab.

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

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

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

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

} else {
postman.setNextRequest(null);
}

Run history. One iteration (run three times).

I think the bit you are missing is the storing of the array. Variables are stored as strings, so you have to Stringify them when storing, and JSON.parse when retrieving otherwise they will just be viewed as a string (not an array).

However I swear that the example I had previously saved and tested for this exact scenario didn’t need to do this.

It took me a while to work out what was going wrong, and amending the storing and retrieving of the array fixed it.

You need to have something in the resources array, or the first line of the pre-request script will error (as it needs something, even if its a blank “” array. It can’t just be empty though.

Pretty sure I didn’t need to do this previously either.

Not sure if something changed with how variables are stored in Postman 10, as it took me while to get this working again but it does work.

thank you very much for your help.
why do you think I’ve got this error?
There was an error in evaluating the Pre-request Script:JSONError: No data, empty input at 1:1 ^

There has to be something in the variable.

Even if its just the square brackets (blank array). Otherwise the JSON.parse will error.