How to grab variable from one API and add it to the endpoint of another api so it is called many times?

I have two issues that i need help with …

  1. In API 1 - i want to loop the response body of GET API and find all the “header” that contain “Delete”

When the script finds something that contains “Delete”, grab the announcementid and set it in an array …

the code below,
is finding the ones that don’t say “Delete”, but the array that should show the announcement ids that have delete in them are not showing i need this to show …

var jsonData = pm.response.json();

var resultCount = jsonData.length;

// Test arrays

var hasDelete = ;

var doesntHaveDelete = ;

// Loop through and set arrays with matching data

for (i = 0; i < resultCount; i++) {

var id = jsonData[i].announcementId;

var modelString = jsonData[i].header.toLowerCase();

if (modelString.includes(“Delete”)) {

hasDelete.push({

  "announcementId": id,

  "hasDelete": modelString.includes("Delete")

});

} else {

doesntHaveDelete.push({

  "announcementId": id

});

}

}

// Check that each object in response contained keyword and length matches from test

pm.test(“Expect response to contain Delete”, function() {

console.log(hasDelete);

console.log(doesntHaveDelete);

pm.expect(hasDelete.length).to.equal(resultCount);

});

2nd issue:
Now i want to add that array of AnnouncementIDs and add it to API 2’ endpoint:

and i want to do this as many times as there is a number within the Announcement id array.

Please help …

Easy fix for your first one. You to toLowerCase() on the header, but the string you’re trying to match has an upper case: Delete.

var jsonData = pm.response.json();
var resultCount = jsonData.length;

// Test arrays
var hasDelete = [];
var doesntHaveDelete = [];

// Loop through and set arrays with matching data
for (i = 0; i < resultCount; i++) {
  var id = jsonData[i].announcementId;
  var modelString = jsonData[i].header.toLowerCase();
  if (modelString.includes('delete')) {
    hasDelete.push( id );
  } else {
    doesntHaveDelete.push( id );
  }
}

// Check that each object in response contained keyword and length matches from test
pm.test(“Expect response to contain Delete”, function() {
  console.log(hasDelete); 
  console.log(doesntHaveDelete);
  pm.expect(hasDelete.length).to.equal(resultCount);
});

pm.collectionVariables.set('deleteIds', JSON.stringify(hasDelete));

i don’t expect your assertion to be true 100% of the time. You’re testing to make sure that every result that comes back in the response has the word delete in the header.

For Part 2:
You want to iterate over each of the hasDelete ids from your first request. So in your pre-request script add the following snippet:

let deleteIds = pm.collectionVariables.get('deleteIds');
if(deleteIds) {
  deleteIds = JSON.parse(deleteIds);
}
else {
  deleteIds = [];
}

let accouncementId = deleteIds.pop();
pm.collectionVariables.set('announcementId', announcementId);
pm.collectionVariables.set('deleteIds', JSON.stringify(deleteIds));

Set the announcementId variable to your query parameter close to what you have in your screenshot (you don’t need the dollar sign).
In your test script, you would add whatever tests you want to assert on, but you will also need to add the following script:

try {
  const deleteIds = JSON.parse(pm.collectionVariables.get('deleteIds'));
  if(deleteIds.length > 0) {
    postman.setNextRequest('120856 - GET DeleteAnnouncement'); // This is the name of the request in Postman
  }
} 
catch(err) {
  console.log(err);
}

The combination of the pre-request script and the test script will allow you to iterate over all the ids in the deleteIds variable.

Thank you so much the first part is working perfectly,

the second part i am getting this error:

Also the last try and catch condition that should go in the test tab of API2 correct?

You have a typo in line 3:

deleteIds = JSON.parse(deleteIds);

And yes, the try catch goes in the tests tab of the second request

Thanks, did what you said , but now i am seeing a 404 error … it seems the announcementId is not coming up as a variable …

i changed collection to environment just to show you:

You’re saving the ids as objects.

The example I provided was saving them as strings, which is why what I provided isn’t working for you.

If you wish to continue saving them as objects, that’s fine, you’ll just need to access the id like this:

pm.environment.set('announcementId', announcementId.announcementId);

Ahh got ya, how would i save the id as something other than object?

I did try this its working but its only sending a request to the first announcementId
its not looking at the whole array … (11)

The code snippet i provided above will save it as an integer.

Are you using the prerequest script I provided? It loads the ids, removes the first one, and uses that in the request. Then it saves the updated array.

That process repeats until there are no more.

Hi I changed the object to int as you had described for the first API,

and followed your steps closely for the pre req and test scripts on the 2nd API:

But i am getting this issue now:

Ah this was my fault. I made a typo in the prerequest script. I had it spelled accouncementId instead of announcementId.

The above script should make it start working.

Hi did exactly that… now getting a 500 error… i know its not the security since the other api are showing 200

and if i change the announcementid varible to a number it works fine … its something with the announcementid var

does it work if you manually type in 171 in the url instead of using the variable? The console makes them look the exact same to me.

Yes, if i add 186 manually at the top within the url it is working fine,
its not working if i add the variable.

But what if you add 171 at the top? I’m asking specifically about that value.

I just executed it right this moment, if I add 170 at the top which came from the array,
it is working fine

Hi so i tried this…

  1. Added a slide through a post request
  2. Got the Posted request and added it in a Array in the Get command
  3. Used the delete and was able to delete one of the sectionids , but i want it to delete all the sectionids by clicking send once …
    i clicked send again and than it deleted another sectionid

how can i make it so it deletes all the sectionids within that array at one click?



You can’t. The method I provided is for running through the collection runner or a monitor/newman.

The alternative would be to update the API to take in multiple values.

1 Like

Oh got ya… so we cant add a loop that specifies if that array has 7 ids - send request to all 7 ids …?

You can, but it has to be through the collection runner.

Hitting send is going to fire the one request only.

Of course you can add code in there like pm.sendRequest, but I don’t think that’s going to get you what you’re after.

Also you can do is save those values in variable and delete it within loop. And sorry Allen flagged ur post by mistake or use pre script