1

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}} ?

asked Sep 18, 2019 at 8:20

1 Answer 1

1

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}}
}
answered Sep 18, 2019 at 8:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.