fuzzi
fuzzi

Reputation: 2277

Postman environment variable returning null

I want to add a payload schema to an environment variable so that I can validate the response payload against the schema.

I have my environment variable defined as follows:

responseSchema: 
{ "properties":
      "firstname":
         {"type":"string" },
       "lastname": 
          {"type":"string"},
       "phonenumber": 
          {"type":"integer","format":"int64"}
       },
   "required":["firstname", "lastname", "phonenumber"]}

However, I can not access this environment variable within my Postman Test code. I've tried accessing it by:

environment.responseSchema

However, this returns null. How can I access the environment variable that I've created using postman. The way I have implemented this is consistent with http://blog.getpostman.com/2017/07/28/api-testing-tips-from-a-postman-professional/ TIP #4: JSON Schema validation

Upvotes: 1

Views: 3012

Answers (1)

Andrew L
Andrew L

Reputation: 5486

So to be clear you are adding a collection variable, not an environment variable. More on Postman variables

To access your collection variable you can do pm.variables.get("responseSchema") in the Tests script tab.

To be a bit more complete you should parse it as well.

var mySchema = JSON.parse(pm.variables.get("responseSchema"));
console.log(mySchema.properties.firstname.type);

Also I believe your object is invalid you probably meant to do

{
    "properties": {
        "firstname": {
            "type": "string"
        },
        "lastname": {
            "type": "string"
        },
        "phonenumber": {
            "type": "integer",
            "format": "int64"
        }
    },
    "required": ["firstname", "lastname", "phonenumber"]
}

Upvotes: 4

Related Questions