Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Postman - set a variable from an array for each request

I'm trying to set tests for my APIs collection. The test should call three times, with three different parameters for each request in the collection.

I have added a pre-request script to my collection, containing two requests:

let countryIso3List = pm.collectionVariables.get("CountryIso3List");

if(!countryIso3List || countryIso3List.length == 0) {
    countryIso3List = ["AFG", "SYR", "IDN"];
}

let countryIso3 = countryIso3List.shift();

pm.collectionVariables.set("CountryIso3", countryIso3);
pm.collectionVariables.set("CountryIso3List", countryIso3List);

Then in the test of the requests I have the following code:

const currentCountryIso3List = pm.collectionVariables.get("CountryIso3List");
if (currentCountryIso3List && currentCountryIso3List.length > 0){
    postman.setNextRequest(request.name);
} else {
    postman.setNextRequest(null);
}

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

It works fine only for the first call, putting "AFG" in the variable CountryIso3 which is then used to make the request.

After this initial run, I see that the CountryIso3List contains "SYR,IDN" as string and it fails to make the shift() command and it stops.

I've tried modifying the Pre-Request script by adding a split on the collection variable:

let countryIso3List = pm.collectionVariables.get("CountryIso3List");

if(!countryIso3List || countryIso3List.length == 0) {
    countryIso3List = ["AFG", "SYR", "IDN"];
}
else{
    countryIso3List = pm.collectionVariables.get("CountryIso3List").split(',');
}


let countryIso3 = countryIso3List.shift();
pm.collectionVariables.set("CountryIso3", countryIso3);
pm.collectionVariables.set("CountryIso3List", countryIso3List);

but it works fine on the first cycle, then if I repeat the test it brokes saying:

TypeError: pm.collectionVariables.get(...).split is not a function

How can I fix it?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!