'Postman - Take the last id from response of a Get request and assign it to variable
I have to make a request to an endpoint with a non-existing id. The endpoint with all users returns a user list with the following structure:
{
"page": 1,
"per_page": 10,
"total": 100,
"total_pages": 1000,
"data": [
{
"id": 1,
"email": "a@b",
"first_name": "a",
"last_name": "b",
"avatar": "jpg"
}
]
}
I have to find the last returned id, increment it +1 and assign it to a variable.
Solution 1:[1]
You can use the Lodash last method within the sandbox, to get the id of the last object in the data array.
pm.globals.set('id', _.last(pm.response.json().data).id);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Danny Dainton |
