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

344
Views
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 answers
Answer question

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 Report
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!