Postman: how to evaluate json arrays

Viewed 13322

With Postman it is possible to save a special field from the response body into a variable and use the value of this variable in a consecutive call.

For example: in my first call to the webservice the following is returned in the response body

[ {
  "id" : "11111111-1111-1111-1111-111111111111",
  "username" : "user-1@example.com",
}, {
  "id" : "22222222-2222-2222-2222-222222222222",
  "username" : "user-2@example.com"
} ]

I have added a test

postman.setGlobalVariable("user_0_id", JSON.parse(responseBody)[0].id);

Now I send a consecutive request to the webservice with the URL

http://example.com/users/{{user_0_id}}

Postman evaluates {{user_0_id}} to 11111111-1111-1111-1111-111111111111.

This works fine. But now I add to the test of my first call

postman.setGlobalVariable("users", JSON.parse(responseBody));

In my second request to the webservice I call the URL

http://example.com/users/{{users[0].id}}

But now {{users[0].id}} cannot be evaluated, it stays the same and is not replaced by 11111111-1111-1111-1111-111111111111.

What can I do? What is the correct syntax of the call?

1 Answers
Related