Store IDs from json and re-use them in a second API call

Hi @francoisc,
Welcome to the community :wave:

You can make use of Postman’s variables and collections for this. Create a collection with your two requests (https://learning.getpostman.com/docs/postman/collections/intro_to_collections/)

In your first request’s test script, you can have the following snippet.

let response = pm.response.json(),
    ids = _.map(response.items, ({ id }) => ( id )); // This will store your ids in an array. For reference (https://lodash.com/docs/4.17.11#map)

pm.variables.set('ids', JSON.stringify(ids));

And in the body of the second request, you can access the ids like this. Choose Raw and content type as application/json

{
 "ids": {{ids}},
 "type": "test"
}

Now you can run your collection with the help of Postman’s collection runner (https://learning.getpostman.com/docs/postman/collection_runs/starting_a_collection_run/)

The second request body will have your ids. You can check the requests sent in Postman’s console (View > Postman Console)

https://learning.getpostman.com/docs/postman/collection_runs/debugging_a_collection_run/

1 Like