Search code examples
arraysparametersrequestpostmanresponse

How to send a postman Get-Request with params as response-array of another request


I'm trying to send 2 Get-Requests with Postman in which the response 1 was used for request 2, as follows:

    {   "userId": 1,
        "id": 6,
        "title": "dolorem eum magni eos aperiam quia",
        "body": "ut aspernatur ... "
    },
    {
     ... 

There are many of them. So i have many userId from 1 to 10.

Now i want to take all userId of Response_1 and put it in to Request_2 by using Params in Postman:

enter image description here

How can i take all userId of Response_1 in to the Request_2 with pair Key : Value like that. Many thanks.


Solution

  • in first request test section add:

     pm.environment.set("useridArray",pm.response.json().map((a)=>a.userId))
    

    here we are getting userid from each object using map function , the map function returns an array.

    now in second request prerequest use:

    let testIDArray = pm.environment.get("useridArray")
    pm.environment.set("testID",testIDArray.pop())
    pm.environment.set("useridArray",testIDArray)
    testIDArray.length ? postman.setNextRequest(pm.info.requestName) : postman.setNextRequest(null)