Is there a way to change the id parameter automatically when creating a GET request on Postman, rather than manually creating separate requests

I am quite new to Postman and I am trying to solve the following problem:

I am using the CKAN api and have found a way to get list of active users and their id http://demo.ckan.org/api/3/action/user_list.

I now want to find the last time each user was active using this call http://demo.ckan.org/api/3/action/user_activity_list?id=

These calls were found on ckan’s api guide. API guide — CKAN 2.9.2 documentation

Is there a way to update the id parameter with a loop of some sort, so that i dont have to call separate get requests for each id

Cheers.

Create a folder with the two following requests in it, then run the folder.

Request #1

GET https://demo.ckan.org/api/3/action/user_list

Script Tests:

// this is executed after the request
// the goal is to set all the results in the resultList variable
// and also to set a counter, a current index of the results, in the currentResultIndex variable

// get the response as a JSON object
var jsonData = pm.response.json();
// set the result part of the response (an array) in an environment variable name resultList
pm.environment.set("resultList", JSON.stringify(jsonData.result));
// set another environment variable named currentResultIndex, which is the index of the result array
pm.environment.set("currentResultIndex", 0);

Request #2

GET http://demo.ckan.org/api/3/action/user_activity_list?id={{currentId}}

Script Pre-request Script:

// this is executed before the request
// the goal is to set the currentId variable that is used in the request

// get the result response from the environment variable resultList set in the first request
var resultList = JSON.parse(pm.environment.get("resultList"));
// get the current index of the result array from the environment variable currentResultIndex
var currentResultIndex = pm.environment.get("currentResultIndex")*1;
// get the id of the indexed result
var currentId = resultList[currentResultIndex].id;
// set the id as currentId variable
pm.variables.set("currentId", currentId);

Script Tests:

// this is executed after the request
// the goal is to set the next request to be executed, or end the loop

// get the result response from the environment variable resultList set in the first request
var resultList = JSON.parse(pm.environment.get("resultList"));
// get the current index of the result array from the environment variable currentResultIndex
var currentResultIndex = pm.environment.get("currentResultIndex")*1;
// increment the current index
currentResultIndex++;
// check if the current index has reached the end of the result array
if (currentResultIndex < resultList.length) {
  // there is another result to be requested, update the index in the environment variable
  pm.environment.set("currentResultIndex", currentResultIndex);
  // set the next Postman request to be executed as the current one (the request #2), 
  // hence doing a loop of requests until there is no more indexed result to be requested.
  postman.setNextRequest(request.name);
}
3 Likes

Thanks for your response.

Does this let you see the json response of the second request.
So json output of user activity given the id. Can’t seem to find that.

CHeers