1. 程式人生 > >3--Postman--變數(environment&global)

3--Postman--變數(environment&global)

(1) Environment

clear an environment variable: pm.environment.unset("variable_key")--recommend postman.clearEnvironmentVariable(variableName) postman.clearEnvironmentVariables()   get an environment variable: pm.environment.get("variable_key")--recommend postman.getEnvironmentVariable(variableName)   set an environment variable
: pm.environment.set("variable_key", "variable_value")--recommend postman.setEnvironmentVariable(variableName, variableValue)--string   (2) Global variables   clear a global varibale: pm.globals.unset("variable_key") postman.clearGlobalVariable(variableName) postman.clearGlobalVariables()   set a global variable
: pm.globals.set("variable_key", "variable_value"); postman.setGlobalVariable(variableName, variableValue)   get a global variable: pm.globals.get("variable_key") postman.getGlobalVariable(variableName)   (3) Json & string 
JSON.parse()【從一個字串中解析出json物件】 例子: //定義一個字串 var data='{"name":"goatling"}' //解析物件 JSON.parse(data) 結果是: name:"goatling" JSON.stringify()【從一個物件中解析出字串】 var data={name:'goatling'} JSON.stringify(data) 結果是: '{"name":"goatling"}'   Getting an environment variable (whose value is a stringified object):Json Setting a nested object as an environment variable:String

 

(4) Get a variable

This function searches for the variable across globals and the active environment.

pm.variables.get("variable_key");

 

(5) Dynamic variables

 

Note that dynamic variables cannot be used in the Sandbox. You can only use them in the {{..}} format in the request URL / headers / body.