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:
- Running the first req and reciving in the response the DEPOSITS array, each one with a uniqe id.
- Running the second request, with the first id from the array that I received in the first response.
- 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..)
- 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.
1 Answer 1
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:
-
Anything else that I need to add to the pre-request? cause im getting 'JSONError: Unexpected token u in JSON at position 0'Hila– Hila2021年10月24日 09:07:22 +00:00Commented Oct 24, 2021 at 9:07
-
@Hila please double check your script. I've tested these on my side, no error at all.lucas-nguyen-17– lucas-nguyen-172021年10月24日 11:20:12 +00:00Commented Oct 24, 2021 at 11:20
-
you r right! sorry! And if i want to take it one step further.. if i have a third request that i need to run with the first id.. and only then(!) go back to the second to change to the second id?Hila– Hila2021年10月24日 13:19:55 +00:00Commented Oct 24, 2021 at 13:19
-
Please, edit your question and clear out what you want. You can add examples and flow you’re doing.lucas-nguyen-17– lucas-nguyen-172021年10月24日 14:28:00 +00:00Commented Oct 24, 2021 at 14:28
-
Thank u! (i was sick..sorry for the delay). Ill check if its working for me later on the weekHila– Hila2021年11月01日 19:28:49 +00:00Commented Nov 1, 2021 at 19:28
ids
?