How to loop through array and use its values in a request

I have two API requests, one that creates an array of pull request ID’s (PRIDs) from a git repo, and another that gets a count of comments associated with an individual pull request. I would like to be able to loop through the array of PRIDs to return a total count of existing pull requests (PRIDs.length) and average comments per PR measure (Total comments/PRIDs.length).

I am a very young Postman user (think newborn, I started with it yesterday), but it seems like there might be a way to store the PRID array in a collection variable, then loop through that array with the second request to obtain the desired counts, I just don’t know how to use the software well enough to do that.

I have a written the test script that creates an array of Pull Request ID’s as such:


var body = JSON.parse(responseBody);
var PRIDs = ;
for (var i = 0; i<body.value.length;i++){

And have written the test script that counts the number of comments in a single pull request as so:{organization}/{project}/_apis/git/repositories/{repository}/pullRequests/{PRID}/threads?api-version=5.0

var body = JSON.parse(responseBody);
var count = 0;
for (var i = 0;i<body.value.length;i++) {
if (body.value[i].comments[0].commentType ==“text”) {

Any help, or just a nudge in the general right direction, would be very greatly appreciated, thanks!

Also, if this helps at all, or if anyone has suggestions for another problem I’ve encountered I am using the Azure Devops REST API. I have noticed that when returning the Pull Requests, it caps the $top field at 1000 instances, and since my repo has 2194 Pull Requests thus far, it means I will have to run this script (assuming it is possible) three times to get all of the data I want. If you know of an easy way to automate this into one request that would be splendid!

Hello @shepsims, welcome to the community! :wave:

I can help you get around the first problem that you’re facing regarding storing the PRIDs and using that to get the comments per PR using the second request.

I wrote this collection to help you around with some sample code:

Step 1: We’ll be using the collection runner feature in Postman to get around this problem.
If you need to run it one request at one time manually then you can do that too by tweaking the logic a bit. I am sure you’ll get your head around that once you see the scripts inside the collection that I wrote.

Also, we need to build a control logic which will help us in determining when to stop and when to keep the collection running, so for that I have used the feature of environments and postman.setNextRequest

Step 2: You need to create a new empty environment (you can refer the docs as to how you can create a new environment).

Step 3: Import the collection that I gave the link to above (again - and you can see that there are two requests inside, the first is to fetch the PRIDS and the second is to get comments for each PRID.

Now, what we are gonna do is something like this:

  1. Get all PRIDS
  2. Get comment for each PRID and write some logic around how you plan to use the comments.

Step 3: Launch the collection runner and make sure you select the new environment that we created just now.

Step 4: Run the collection and right now, it uses some sample data that I faked using the Postman Mocks

You’ll see that initially I get an array for 4 PRIds and the collection runner loops over each ID and gets the comment for the same.
This logic will run for n number of PRIDs.

I have documented the Tests scripts of both the requests and I am hoping that by reading the docs and the comments and code, you’ll be able to get around this problem.

All in all, you need to just basically replace the endpoints that I wrote with the actual endpoints and build the request.

Next you can write your logic and get your work done!

If you’re stuck somewhere, let me know!
Happy to help :smiley:


Thank you! This is super useful!

1 Like