简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM