What is the best way to iterate over response's ids in different request?

Hello.
I have request {{host}}:{{port}}/ that returns me some data

{
  "list": [
        {
        "id": 0
    },
    {
        "id": 125,
    },
    {
        "id": 23,
    },
    ...
   }
 ]
}

Also I have another request {{host}}:{{port}}/id

Is it possible using Postman to iterate over ids in Tests Runner?

Yes it is possible. Use below code in your Request: Test tab to print the ids in the console.

var jsonData = pm.response.json();
var count = jsonData.list.length();
for (var i=0; i < count ; i++){
 console.log(jsonData.list[i].id);
}

Thank you.
But the problem is not how to iterate in the current Request but how to call all ID in 2nd Request.

So in 1st Request I get this ID by{{host}}:{{port}}/list
In 2nd I want to iterate over IDs in a single request by {{host}}:{{port}}/{{id}}

I do not want to create N request with variables like {{host}}:{{port}}/{{id_1}}, {{host}}:{{port}}/{{id_2}}, {{host}}:{{port}}/{{id_3}}, etc and using pm.environment.set in 1st Request

@KNikolay Use below code in your Request: Test tab of the first request. (only if you have two requests in your collection. If not use pm.sendRequest( host/port/id request details) refer for more details https://learning.postman.com/docs/postman/scripts/postman-sandbox-api-reference/#pmsendrequest)

var jsonData = pm.response.json();
var count = jsonData.list.length();
pm.environment.set("idsCount", count);
var currentIdPosition = pm.environment.get("idPosition"); 
if (count > 0) {
  if (currentIdPosition == null) {
     pm.environment.set(idPosition, currentIdPosition);
   }
  pm.environment.set("currentIdValue",jsonData.list[currentIdPosition].id);
  postman.setNextRequest("REQUEST NAME of host:port/id");  
}

in the testscript of {{host}}:{{port}}/id add below

var idsCount = pm.environment.get(idsCount);
var currentIdPosition = pm.environment.get(idPosition);
if(currentIdPosition !== idsCount-1 ) {
  postman.setNextRequest("REQUEST NAME of host:port/list");
}

I’m trying to do this and it’s not quite working. It’s going in an endless loop back from the GET all, to the DELETE. What I’m hoping to accomplish is get all entities, collect their IDs into an array variable, and then use that list to call DELETE by id for each entity.

First Request test script:

var jsonData = pm.response.json();
pm.environment.set("idsCount", jsonData.result.length);
var currentIdPosition = pm.environment.get("idPosition"); 
if (pm.environment.get("idsCount") > 0) {
  if (currentIdPosition == null) {
     pm.environment.set(idPosition, currentIdPosition);
   }
  pm.environment.set("currentIdValue",jsonData.result[currentIdPosition]._id);
  postman.setNextRequest("delete all languages");  
}

Second Request:
URL: {{api-host}}/v1/languages/{{currentIdValue}}

Script:

var idsCount = pm.environment.get(idsCount);
var currentIdPosition = pm.environment.get("idPosition");
if(currentIdPosition !== idsCount-1 ) {
postman.setNextRequest("all languages");
} else {
postman.setNextRequest(null);
}

The delete is happening, but then it just keeps going back and forth from GET to DELETE. In the example above from Feb. 2020, is the reason we go back to the first call to increment to the next item in the array? If not, how is that accomplished? Do we have to go back to the GET call? Or should we not?

Please advise, thank you!

Hi @jen,

This line doesn’t look quite right to me:

As per your other get examples, if you want to retrieve the count successfully, you’ll need to put the name of the environment variable in quotes - i.e. get("idsCount")

At the moment, you’re asking it to retrieve a value which (assuming nothing else is in your script) resolves to an undefined value, so hopefully that’s all you need to fix. :crossed_fingers:

Hi. Thanks for the reply @neilstudd
I agree that’s incorrect, but it’s just an oversight. My main issue is trying to figure out how to advance the current id between calls so that I can delete each item by id. Essentially what I’m trying to accomplish is to clear the backend database before each test. So I want to get all resources, and then delete them one by one based on the list of IDs I get from the GET all call.

I just got it to work. :slight_smile:
This probably needs some cleanup, but for those interested, this did what I wanted:

GET all test script:

pm.globals.clear()
var jsonData = pm.response.json();
let languageIds = jsonData.result.map((i) => i._id);
pm.globals.set("languageIds", languageIds);

if (languageIds && languageIds.length > 0){
    pm.globals.set("currentIdValue", jsonData.result[0]._id);
    postman.setNextRequest("delete all languages");
} else {
    postman.setNextRequest(null);
}

DELETE request:
URL: {{api-host}}/v1/languages/{{currentIdValue}}
Script:

const languageIds = pm.globals.get("languageIds");

if (languageIds && languageIds.length > 0){
    postman.setNextRequest("get all languages");
} else {
    postman.setNextRequest(null);
}

I do not normally write JavaScript, and I see there are some inconsistencies here, so will work on cleaning this up. But it’s functionally doing what I want. :slight_smile:

1 Like