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

346
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
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