3

I am using Postman to iterate through a json of about 40 pairs of items. I need to then take that array created and run an API call for each element in the array to return a set of results. Using the code here, i'm only able to pull the final element in the array. I attempted to put the postman.setNextRequest in the for loop but then I found out that no matter where it is, it always executes last.

tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);

if (responseCode.code === 200) {

var jsonData = pm.response.json();
var json = [];

postman.setEnvironmentVariable("json", jsonData)
postman.setNextRequest('GetAdmins');

for (var key in jsonData ) {
    if (jsonData.hasOwnProperty(key)) {
        postman.setEnvironmentVariable("organizationId", jsonData[key].id)
        postman.setEnvironmentVariable("orgname", jsonData[key].name)
        tests[jsonData[key].name + " " + jsonData[key].id] = !!jsonData[key].name;
        }
    }
}

else {
    postman.setNextRequest(null);
}

GetAdmins is another GET that uses {{organizationId}} in the call.

I think what i'm looking for is; what is the best way to go about running another API call on each element in the json?

Thanks in advance!

EDIT: Adding JSON output

[
    {
        "id": XXXXXX,
        "name": "Name1"
    },
    {
        "id": XXXXXX,
        "name": "Name2"
    },
    {
        "id": XXXXXX,
        "name": "Name3"
    }
]
user9129112
  • 61
  • 1
  • 7
  • What’s the `var json = []` doing here. It doesn’t seem like you’re pushing anything to it. It looks like each time it runs it would just override the previous env variable set. Also, you could look into using Lodash in Postman to map those values to the keys. – Danny Dainton Dec 22 '17 at 07:27
  • Could you also add the output from `pm.response.json()` to the question please. – Danny Dainton Dec 22 '17 at 09:35
  • @DannyDainton - the var json is being used as the environment variable to pass between API calls. I will check out Lodash. – user9129112 Dec 24 '17 at 04:57
  • I can see that `json` Postman variable being set but you have a `var json = []` in the line above that which has nothing to do with it. It looks like that’s trying to collect something in an array but it not doing anything. – Danny Dainton Dec 24 '17 at 07:06

2 Answers2

2

This might work to get the data - I’ve not tried it out yet though so it might not work first time.

var jsonData = pm.response.json()

data = _.map(jsonData, item => {
         organizationId: item.id
         orgName: item.name
     })
pm.environment.set('organizationData', JSON.stringify(data))

Then you have all of your organization data in a variable and you can use these to iterate over the Id’s in the next "Get Admins" request.

You would need to have some code in the Pre-request script of the next request to access each of the id’s to iterate over in the request. You need to parse the variable like this:

var orgID = pm.environment.get(JSON.parse("organizationData"))

Then orgID[0].organizationId would be the first one in the list.

Not a complete solution for your problem but it might help you get the data.

Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
1

I was able to solve this using these two guides:

  1. Loops and dynamic variables in Postman: part 1
  2. Loops and dynamic variables in Postman: part 2

I also had to implement the bigint fix for java, but in Postman, which was very annoying... that can be found here:

  1. Hacking bigint in API testing with Postman Runner Newman in CI Environment
  2. Gist

A lot of google plus trial and error got me up and running.

Thanks anyway for all your help everyone!

This ended up being my final code:

GetOrgs

tests["Status code is 200 (that's good!)"] = (responseCode.code === 200);

eval(postman.getGlobalVariable("bigint_fix"));

var jsonData = JSON.parse(responseBody);
var id_list = [];

jsonData.forEach(function(list) {
    var testTitle = "Org: " + list.name + " has id: " + JSON.stringify(list.id);
    id_list.push(list.id);
    tests[testTitle] = !!list.id;
});

postman.setEnvironmentVariable("organizationId",JSON.stringify(id_list.shift()));
postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
postman.setNextRequest("GetAdmins");

GetAdmins

eval(postman.getGlobalVariable("bigint_fix"));

var jsonData = JSON.parse(responseBody);

jsonData.forEach(function(admin) {
    var testTitle = "Admin: " + admin.name + " has " + admin.orgAccess;
    tests[testTitle] = !!admin.name;
});

var id_list = JSON.parse(environment.id_list);
if (id_list.length > 0) {
    postman.setEnvironmentVariable("organizationId", JSON.stringify(id_list.shift());
    postman.setEnvironmentVariable("id_list", JSON.stringify(id_list));
    postman.setNextRequest("GetAdmins");
}

else {
    postman.clearEnvrionmentVariable("organizationId");
    postman.clearEnvironmentVariable("id_list");
}
n-verbitsky
  • 552
  • 2
  • 9
  • 20
user9129112
  • 61
  • 1
  • 7
  • What’s the bigInt fix doing in your context? All of the solutions are using the older postman syntax so I would advise checking out the pm.* api as this older syntax will be dropped eventually. – Danny Dainton Dec 24 '17 at 09:09
  • @DannyDainton - Sorry for the, erm, late reply. Bigint was used because the values being returned were very, very large. Without it, Java/Postman was not able to interpret the values it was seeing. – user9129112 May 31 '21 at 16:24