Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda