Using postman.setEnvironmentVariable("host", jsonData.host) sets host to NULL

I want to extract a value from the response and store in a Variable. So I use:

https://postman-echo.com/get

Which gives me the Response:

{
    "args": {},
    "headers": {
        "x-forwarded-proto": "https",
        "host": "postman-echo.com",
        "accept": "*/*",
        "accept-encoding": "gzip, deflate",
        "cache-control": "no-cache",
        "cookie": "sails.sid=s%3A1e1Mmpz_0cZOHSvJ7PECnByndH6aCOkO.aKuAswovLqNDM%2FJsO7h6ptziVJZluWAuqC0gAUsEdlY",
        "postman-token": "e61900c7-7827-4078-b56d-ec0a50e6f7da",
        "user-agent": "PostmanRuntime/7.6.0",
        "x-forwarded-port": "443"
    },
    "url": "https://postman-echo.com/get"
}

And my Test script is:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("host", jsonData.host);

Why is the Variable not set with the correct content?

Thanks

Hey @psorense. First of all, welcome to the community. :smile:

Coming to your question, the host property is nested inside the headers object. You’re trying to access it directly from jsonData.

Here’s what your script should look like:

var jsonData = JSON.parse(responseBody);

postman.setEnvironmentVariable("host", jsonData.headers.host);

Hope this helps.

Great! It works :slight_smile:
Would you also have a solution for how to get the value from a key that has spaces in name like:

"values": {
        "Request Number": "REQ000000023077"
}

postman.setGlobalVariable("REQ_InstanceId", jsonData.values.Request Number);

I’ve tried with [,{,’, and "
But everything gives an error.

Hey @psorense. This should work.

postman.setGlobalVariable("REQ_InstanceId", jsonData.values['Request Number']);

Hey @Alexios,

Welcome to the community :wave:

As that property is inside an object in the data array ([]), you would need to add this to your reference path. The data array looks like it only has a single object ({}) so you would reference that with [0] as that would be the first item in the zero-indexed list.

pm.test("Check if Device with currentStatus is online", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.data[0].currentStatus).to.eql("online");
});

The way you have it, it would be looking for the currentStatus property in the 17th object in the array of the response body.

Here’s an example of what that would look like:

[
    {
      // Object 1
    },
    {
      // Object 2
    },
    {
      // Object ...
    },
    {
      // Object 15
    },
    {
      // Object 16
    },
    {
      "currentStatus": "online"
    }
]

Hope that helps :slight_smile:

What is the reference you are making for that item? What does your test script look like?

this is my script:

pm.test("Check if Device with udid is 62652651265asdasdasd", function () {
    var jsonData = pm.response.json();
    console.log(jsonData);
    pm.expect(jsonData[1].udid).to.eql("62652651265asdasdasd");
});

As I mentioned in my first reply, that won’t work because those properties are in the data array.

You need to include that in the reference, take a look at the test script that I sent you.

1 Like

@danny-dainton
Thanks a lot man for all ur help i solve the problem with ur help

1 Like