https://learning.postman.com/docs/sending-requests/variables/ https://learning.postman.com/docs/sending-requests/variables/#defining-variables-in-scripts https://learning.postman.com/docs/sending-requests/managing-environments/

Defining variables in scripts
全局变量
// 语法pm.globals.set("variable_key", "variable_value");
示例
集合的变量
// 语法pm.collectionVariables.set("variable_key", "variable_value");
示例
环境env变量
// 语法pm.enviroment.set("varible_key", "variable_value");
示例
本地临时变量
// 语法pm.variables.set("variable_key", "variable_value");

在前、后置脚本中,使用变量
访问变量
脚本中发起网络请求
pm.sendRequest(func_name, function(err, res) { };
// 设置全局变量(名称全大写)const CURRENT_ENV = pm.globals.set('CURRENT_ENV', 'design');// 设置环境变量const current_env = pm.environment.set('current_env', 'design');function sendLoginRequest() {const mobile = pm.environment.get("mobile");const password = pm.environment.get("password");const app_id = pm.environment.get("app_id");const loginRequest = {url: "https://www.xxxxx.com/ccccc.do",method: "POST",header: {"Content-Type": "application/json;charset=UTF-8",},body: {mode:'raw',raw:JSON.stringify({"mobile":mobile,"password":password,"appId":app_id})}};pm.sendRequest(loginRequest, function(err, res) {console.log("登录的 mobile: " + mobile)console.log("访问的 app_id: " + app_id)if (err) {console.log("===========> 错误信息:" + err + "<===========");} else {const jsonData = res.json();console.log("===========> loginRequest 请求响应体: " + res.text() + "<===========")pm.environment.set("access_token", jsonData.data.access_token);pm.environment.set("refresh_token", jsonData.data.refresh_token);pm.environment.set("access_token_expires",jsonData.data.expires_in);pm.environment.set("authorization",(jsonData.data.type + " " + jsonData.data.access_token));}})};function sendRefreshRequest() {const app_id = pm.environment.get("app_id");const access_token = pm.environment.get("access_token");const refresh_token = pm.environment.get("refresh_token");const refreshToken = {url: "https://www.xxx.com/aaaaa.do",method: "POST",header: {"Content-Type": "application/json;charset=UTF-8", // 注意:header 需要加上 Content-Type},body: {mode:'raw',raw:JSON.stringify({"app_id":app_id,"access_token":access_token,"refresh_token":refresh_token})}};pm.sendRequest(refreshToken, function(err, res) {// console.log("登录的 mobile: " + mobile);console.log("访问的 app_id: " + app_id);if (err) {console.log("===========> 错误信息:" + err + "<===========");} else {const jsonData = res.json();console.log("===========> refreshToken 请求的响应体: " + res.text() + "<===========")pm.environment.set("access_token", jsonData.data.access_token);pm.environment.set("refresh_token", jsonData.data.refresh_token);pm.environment.set("access_token_expires",jsonData.data.refresh_expires_in);pm.environment.set("authorization",(jsonData.data.type + " " + jsonData.data.access_token));}});}var accessToken = pm.environment.get("access_token");var refreshToken = pm.environment.get("refresh_token");var accessTokenExpires = pm.environment.get("access_token_expires");var authorization = pm.environment.get("authorization");console.log("当前的时间: " + Date.parse(new Date())/1000);console.log("返回的过期时间: " + accessTokenExpires);console.log("access_token 值: " + accessToken);console.log("refreshToken 值: " + refreshToken);console.log("authorization 值:" + authorization);// 如 ACCESS_TOKEN 没有值,则执行发送登录接口请求; ACCESS_TOKEN_EXPIRES 已过期,则执行发送刷新token的接口请求if ( !accessToken ) {console.log("没有token值,进行登录调用")sendLoginRequest();} else if (accessTokenExpires <= Date.parse(new Date())/1000) {console.log("token过期了,进行会话续期调用")sendRefreshRequest();} else {console.log("当前token有效")}
参数化传递/上下文依赖取值 retrieve the value
https://learning.postman.com/docs/writing-scripts/intro-to-scripts/ https://learning.postman.com/docs/writing-scripts/pre-request-scripts/ http://www.postmanlabs.com/postman-collection/RequestBody.html https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/
前置脚本
pm.request.bodypm.request.body.toString()pm.request.body.raw // 如果是空,则为 undefined// 转化请求体中(有占位的)数据pm.variables.replaceIn(body_raw);
后置脚本-获取本接口请求中的参数
获取本接口请求中的参数,提供给下一个接口用
// 获取 Query 参数对象var queryParams = pm.request.url.query;// 获取 key 为 field1 的 query 参数的值var field1 = queryParams.get("field1");// 已键值对象方式获取所有 query 参数var quertParamsObject = queryParams.toObject();// 遍历整个 queryqueryParams.each((item) => {console.log(item.key); // 输出参数名console.log(item.value); // 输出参数值});// 增加 query 参数queryParams.add({key: "field1",value: "value1",});// 修改 query 参数(如不存在则新增)queryParams.upsert({key: "field2",value: "value2",});// 当 body 类型为 form-data 时,从 pm.request.body.formdata 获取请求参数var formData = pm.request.body.formdata;// 获取 key 为 field1 的 form-data 参数的值var field1 = formData.get("field1");console.log(field1); // 控制台打印 field1// 当 body 类型为 x-www-form-urlencode** 时,从 pm.request.body.urlencoded 获取请求参数var formData = pm.request.body.urlencoded;
// 当 body 类型为 json 时,从 pm.request.body.raw 获取请求参数var jsonData = JSON.parse(pm.request.body.raw); // 获取请求体JSON对象title = jsonData.Namepm.environment.set('schematitle',title);console.log(schematitle); // 控制台打印参整个 json 数据}
后置脚本-获取本接口响应中的参数
pm.response.json()//或 JSON.parse(responseBody)
下游接口引用上游的响应参数
URL 参数化
https://www.w3school.com.cn/js/js_string_templates.asp http://www.postmanlabs.com/postman-collection/Request.html

// 环境变量中设置了 password 等值function sendLoginByPassword(){const password = pm.environment.get("password")const appId = pm.environment.get("app_id")const rsaEncrypt = {// 引用变量passwordurl: `https://www.xxx.com.cn/tool/rsaencrypt?value=${password}`,method: "GET",header: {"Content-Type": "*/*"},};pm.sendRequest(rsaEncrypt, function(err, res) {if (err) {console.log(err)} else {const jsonData = res.json();console.log(jsonData)}})}sendLoginByPassword()


