Search code examples
javascriptpostmanpostman-testcase

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

  • 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);