How to update environment variables based on a response in Postman

Written by ppolyzos

Software engineer based in Athens, Greece

19 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.

  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

Leave a Comment

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