Postman array iteration from previous request

Hi everyone,

I have question about chaining requests. My goal is to modify by API a list of repository (with a PATCH request) but I am getting this list by an other API request (with a GET request).

I made the fisrt GET request with success to get list of my repository and have a response like this :
{
“repositories”: [
{
“id”: “32191961-73f1-498e-a988-66dae2ddd8e5”,
“namespace”: “namespace1”,
“namespaceType”: “organization”,
“name”: “repo1”,
“shortDescription”: “”,
“visibility”: “public”,
“scanOnPush”: true,
“immutableTags”: false,
“enableManifestLists”: false,
“pulls”: 361,
“pushes”: 264,
“tagLimit”: 0
},
{
“id”: “58668dc9-da9e-44b8-a113-13e94007a209”,
“namespace”: “namespace1”,
“namespaceType”: “organization”,
“name”: “repo2”,
“shortDescription”: “”,
“visibility”: “priivate”,
“scanOnPush”: false,
“immutableTags”: false,
“enableManifestLists”: false,
“pulls”: 0,
“pushes”: 268,
“tagLimit”: 0
},

But I don’t find a way to launch back my request for each values of the repositories array gotten in response.

The solution I found but I think is not very elegant is :
To push in a array all the repo name I want to change and create a CSV and use it as the data file of my collection runner. What is not OK for me is that I need to edit manually the text to create the CSV file so I cannot automate the modification

Is there a proper way to do this in postman ?

This is the code that I put in the GET request to create my environment variable :
var jsonData = JSON.parse(responseBody);

var repo2change = [];

jsonData.repositories.forEach(function(repo) {
if (repo.visibility != “public”) {

    repo2change.push(repo.name);
    }

});
pm.environment.set(“repo2change”, JSON.stringify(repo2change, null, 2));

Best regards,

Guillaume MARTIN

Alternatively, you can cram an object into your environment variables. I don’t know that there’s a better way than storing and retrieving it currently.

Hey @Guillaume_MARTIN. Sorry for the delay in response.

If I understand your use case correctly, you’d like to fetch records from one endpoint and then based on the retrieved data, make another request for each retrieved item. Here’s how you can probably do it by using environment variables and looping.

  • First request - fetching all repositories
var response = pm.response.json(),
    repos = response.repositories,
    currentRepoIndex = pm.environment.get("CURRENT_REPO_INDEX") || 0;

pm.environment.set("REPOS", repos);

if (currentRepoIndex === repos.length - 1) {
    postman.setNextRequest(null);
}

postman.setNextRequest('Second Request');
  • Second request - Use the current repo
var repos = pm.environment.get('REPOS'),
    currentRepoIndex = pm.environment.get('CURRENT_REPO_INDEX'),
    currentRepo = repos[currentRepoIndex];
    
// Use the current repo here, let's say in pm.sendRequest

pm.environment.set('CURRENT_REPO_INDEX', currentRepoIndex + 1);

postman.setNextRquest('First Request')

You can learn more about setNextRequest here. Feel free to reach out in case you have additional queries.

1 Like

Thanks, a lot this is exactly what I was looking for.
And it’s far better that I intended to do :slight_smile: