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

347
Visualizações
How to loop through array and use the values in the next request (each time with different value) - postman

My response inculde an array. I need to use each time different id number in the next req. For exp.. if my first response is:

{
  "params": {
    "Deposits": [
      {
        "id": "23",
        "name": "blue"
      },
      {
        "id": "54",
        "name": "pink"
      }
    ]
  }
}

I need to take at first the value of the first deposit' id (which is 23) and implement it in my next req, run it, and then run it again but this time with the next id value (which is 54)

I've tried to use a 'for' loop in my first request 'test'

const responseJson = pm.response.json();
var Products = responseJson.params.Deposits;
for (var i=0; i<Products.length; i++)
{
 postman.setEnvironmentVariable(Products[i].id)
}

and in my second request body i used

{
"id": "{{Id}}"  // neither{{Products[i].id}} worked. At first should be eql to 23 and then 54
"Amount": 2
"date" : null
}

But...its not working. Im missing something.. How do i use the different id's values in the next req?

Edited: My collection has a total of 4 requests. The flow is:

  1. Running the first req and reciving in the response the DEPOSITS array, each one with a uniqe id.
  2. Running the second request, with the first id from the array that I received in the first response.
  3. Running the 3rd and then 4th request (there's no "id" that i need to implement there, just to run it with some different info of the choosen deposit..)
  4. Go back to the 2nd request, change to the next id number from the array, and move on again to the 3rd and then to the 4th request

So It's basically running request 1 > 2> 3> 4 > 2> 3> 4> 2 > 3 >4 >2... and each time a different deposit is choosen.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

UPDATE:

support flow 1 > 2> 3> 4 > 2> 3> 4> 2 > 3 >4 >2... when using Postman runner.

Request 1: get array of ids

Tab Test

const res = pm.response.json();
const ids = _.pluck(res.params.Deposits, 'id');
pm.environment.set('ids', JSON.stringify(ids));

Request 2: get id from ids

Tab Pre-request

const ids = JSON.parse(pm.environment.get('ids'));
pm.environment.set('id', ids.shift());
pm.environment.set('ids', JSON.stringify(ids));

Tab Body

{
  "id": "{{id}}",
  "Amount": 2,
  "date": null
}

Request 3: nothing

Request 4: add logic, if running out of ids, stop flow. Else, continue with request 2.

const ids = JSON.parse(pm.environment.get('ids'));

if (ids.length === 0) {
    postman.setNextRequest(null);
} else {
    postman.setNextRequest("Req2")
}

Result:

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
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