Reputation: 89
I have a Postman POST request, where response body looks like this:
{
"data": [
{
"object": "Answer",
"id": 507,
...
},
{
"object": "Answer",
"id": 208,
...
}
],...
In following DEL request this ids should be used in body as array:
{
"ids": [id1, id2]
}
How can i get these ids from response and store it as environment variable array [id1, id2]
so then it could be used like "ids": {{answer_ids_array}}
?
Upvotes: 1
Views: 696
Reputation: 25921
To capture the id
values as an array and set in an environment variable you could add something like this to the Tests
tab of the first request:
let myArray = []
_.each(pm.response.json().data, (item) => {
myArray.push(item.id)
})
pm.environment.set("idArray", myArray)
To use the array in the request body, you would need to add this to the Pre-request script
to transform to saved string back into an array:
pm.environment.set("ids", JSON.stringify(pm.environment.get("idArray")))
Your request body would then be something like this:
{
"ids": {{ids}}
}
Upvotes: 1