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);
}
When you run the folder, before executing you must check “Save responses”.
Then you will be able to see the response body of request #1 and #2 for all the iterations.