How to update environment variables based on a response in Postman

Written by ppolyzos

Software engineer based in Athens, Greece

36 Comments

  1. chenna

    How do you select 001-0000084765 from below JSon response.

    Tried this code but doesn’t work

    var jsonData = JSON.parse(responseBody);
    postman.setEnvironmentVariable(“HierarchyId”,jsondata.HierarchyId);

    1. ppolyzos

      To access hierarchyId you can do the following:

      One thing that might help you is Postman console. You can open it from View -> Show Postman Console and then you use console.log to help you. More info here

  2. Jhuang

    how to parse an xml response:

    10:00:c4:f5:7c:52:2a:18

    I tried this and it did not like the last response.fibrechannel-switch.name:
    var jsonData = xml2Json(responseBody);
    var response = JSON.parse(jsonData);
    postman.setEnvironmentVariable(“switchName”, response.fibrechannel-switch.name);

    thanks.

    1. ppolyzos

      To view the output of your jsonData variable you can use Postman Console. Open it from View -> Show Postman Console and then you use console.log to help you.
      Usually the parsing is a little different than you might expect, but with Console you ‘ll figure out the problem easily.

      1. Subhajit

        I Am bound to get XML response only :

        I want to take “credentials token” and “site id” as my env ver, how could I be able to achieve the same !! please help

        response:

  3. niti

    How to get “Qk4oSI” from the response:

  4. Cain

    How would I get BackCustomerID? I’ve tried:

    1. ppolyzos

      You response is an array, so you need to get the first element:

    1. ppolyzos

      You can include those conditions in your Tests script.

  5. kanna

    {
    “serial”: “123456”,
    “headers”: {
    “Authorization”: “Signature keys=\”/smarId/hello/1234.123445\”,signature=\”www.hello.com/””
    }

    I want only */smarId/hello/1234.123445* as environment variable

    1. ppolyzos

      If only the Signature keys change, then you can do the following:

      You can learn more about string interpolation and template literals in this link

    1. ppolyzos

      Let’s assume you have this request from OpenWeather API:

      To access the ETag value you can use the following code in Tests panel:

      For more info on postman’s response API you can have a look here

  6. Udhaya

    Hi I am new to postman, can we write JSON object in pre-req?
    and use this in the body?

    1. ppolyzos

      In order to that one way would be to do the following:

      Step 1: Stringify your object and assign it to an environment variable in Pre-request Script tab:

      Step 2: In Body use that variable in your object:

      Note the {{}} which are used in order to use this variable.


      postman set object in request body through environment variable

  7. Aditya

    Hi, I was trying to add a test for a 1 call to add auth token,
    Requirement : If 401 error is thrown then add header with Authorization by calling the auth and copying that token and automatically adding it in the call which I am using. Please help me out, I am not able to do this scenario.
    It’s taking the token as null

    Code :

    1. ppolyzos

      Can you please use the Postman Console to see the actual json response in case it’s wrapped in another object? From top level menu, choose View -> Show Postman Console (CMD/CTRL + ALT + C) and console.log(response.json()) before pm.globals.set("jwttoken", response.json().token);.

      Once you have console opened, please add console.log('1/2/3', pm.globals.toObject()) to check if global values change properly and use the number to understand the order everything is executed.

      The last thing I would try is before setting the new value, firstly unset and then set the variable again:

      Your code looks fine, so I bet you might have missed something while accessing or setting global values properly.

      1. Aditya

        Token has been captured but when it’s trying to add it to the new request call, the token is passing has undefined. I have set the jwttoken variable as Global and in the headers have intialized variable as :

        key : Authorization : Bearer {{jwttoken}}

        and in console for undefined :

        jwttoken: undefined

        When I unset and set also same error is seen.

          1. Aditya

            Hey!! I don’t have any same variable at environment level and it didn’t work when changed it to environment , the access token is getting added if I am adding the test at collection level by parsing the data.

            But for the api level, in the last step jwttoken is going as undefined

  8. Aditya

    Hey,
    Last time your idea worked for me thanks for that, I could not find option to reply, so conveying it here.
    And this time I have a doubt with setting up random values to environement level and use it.
    Actually I wanted to add a random value in place of name and namekey and set it in environment level. So I have attached the code. But it’s going as undefined error always. I am not if I can do this with the below code. So my body is always dynamic so I want to generate random values, whenver it runs and set it and use it for other API. Thanks in advance, my api structure :

    1. ppolyzos

      If you want to create an object with all the properties being random on each request you can create an object in Pre-Request script, set it in an environment variable and then use that environment variable in Request body, as shown in the screenshot below:

      postman random values per request

  9. Divanshu

    Hi Can you please let me know how can i store DeviceID value from the given below response:
    {
    “connectionString”: “HostName=devicenet;DeviceId=71014;SharedAccessKey=fbwsdxs +++++=”,
    “username”: “abc@dev.onmic.com”,
    “password”: “abcdf123”
    }

    1. ppolyzos

      If you just want the DeviceId from your response, you can create a test and use the following quick & dirty way:

      One advice though, you should avoid sending sensitive data in your API responses, not a very good practice.

      Hope I helped 🙂

  10. B Ravi Raju

    Hi, I am capturing the few fields from the API response in the Tests Tab using “pm.globals.set(“actualUsername”, jsonData.identifier.identifier);”

    How can I get the value of key: ‘actualUsername’ in other tests of API?

    1. ppolyzos

      If your value is already set in global variables you can access it through pm.globals.get("actualUsername") and use it throughout all your other tests. As your value is previously stored in Global variables, it will be available in your workspace for all your APIs.
      You can learn more here

Leave a Comment

Your email address will not be published. Required fields are marked *