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:
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.
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")
}