Collection Variable not working

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Hereā€™s an outline with best practices for making your inquiry.

Iā€™m trying to store a value from the response as a collection variable.

I use the command, pm.collectionVariables.set(ā€œdetails_idā€, responseJson.Details.id); in a test function.

Iā€™ve tried two different requested to try and get the information. One is a GET request which gives the error Cannot read property ā€˜idā€™ of undefined. The other is a PUT request which doesnā€™t give an error but when I look at the collection variables the current variable field is empty.

1 Like

Hey @jamesrollinsjr - welcome to the community!

I might be missing some context here but is responseJson the response youā€™ve saved before passing in the id key as a variable?

I tried to reproduce the issue but was able to store a value from the request response as a collection level variable. Hereā€™s the snippet I used and a link to the public workspace/collection that youā€™re more than welcome to fork.

// Log response value in console - args.id is specific to the response I'm working with here.
console.log(pm.response.json().args.id);

// Set response value as a colleciton variable
pm.collectionVariables.set("details_id", pm.response.json().args.id)

The mocked up response here:

{
    "args": {
        "id": "2f9b2d80-8e11"
    },
    "headers": {
        "x-forwarded-proto": "https",
        "x-forwarded-port": "443",
        "host": "postman-echo.com",
        "x-amzn-trace-id": "Root=1-613be75f-3c314d1d7d948138406c0d19",
        "user-agent": "PostmanRuntime/7.28.4",
        "accept": "*/*",
        "postman-token": "9c98346c-6102-49bf-9b7e-703300400a2a",
        "accept-encoding": "gzip, deflate, br",
        "cookie": "sails.sid=s%3ArIXjmz1CY1sQJjmr7Ag1KFJPbtVTaLjI.NAeEjIgLwMoi7pMm8w1ByyoRSXKKSJc6bCf5qWd%2FOYI"
    },
    "url": "https://postman-echo.com/get?id=2f9b2d80-8e11"
}

Public Workspace

Yes, the full script is as such:

pm.test(ā€œGet Message Numberā€, () => {

const responseJson = pm.response.json();



pm.response.to.have.status(200);

pm.collectionVariables.set("details_id", pm.response.json().Details.id);

});

Hi @jamesrollinsjr :wave:

In the response ā€œDetailsā€ is an array, so it may work with Details[0].

pm.collectionVariables.set("details_id", pm.response.json().Details[0].id);

Parsing the JSON response is one of the common challenges we face. Welcome to the club :blush: But this will get better once you are used to JSON responses.

I suggest you to use https://jsonpathfinder.com/, this helped me a lot during my initial testing days. Also I wrote a small blog here about it, feel free to read and provide your feedback! All the best :partying_face:

1 Like

I originally tried that with the GET request and it didnā€™t work so I didnā€™t think to try it in the PUT request which does work. Any ideas why the GET request acts differently?

@jamesrollinsjr Yes for GET request, the response structure might be different. so the same snippet might not work. And I believe as per the documentation you should be using ā€œPUTā€ request type for your request :wink:

So finally is it working? And for future response feel free to check them in https://jsonpathfinder.com/