Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

212
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda