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/

image.png


Defining variables in scripts

全局变量

  1. // 语法
  2. pm.globals.set("variable_key", "variable_value");

示例
image.png


集合的变量

  1. // 语法
  2. pm.collectionVariables.set("variable_key", "variable_value");

示例
image.png


环境env变量

  1. // 语法
  2. pm.enviroment.set("varible_key", "variable_value");

示例
image.png


本地临时变量

  1. // 语法
  2. pm.variables.set("variable_key", "variable_value");

image.png


在前、后置脚本中,使用变量

image.png

访问变量

image.png

脚本中发起网络请求

pm.sendRequest(func_name, function(err, res) { };

  1. // 设置全局变量(名称全大写)
  2. const CURRENT_ENV = pm.globals.set('CURRENT_ENV', 'design');
  3. // 设置环境变量
  4. const current_env = pm.environment.set('current_env', 'design');
  5. function sendLoginRequest() {
  6. const mobile = pm.environment.get("mobile");
  7. const password = pm.environment.get("password");
  8. const app_id = pm.environment.get("app_id");
  9. const loginRequest = {
  10. url: "https://www.xxxxx.com/ccccc.do",
  11. method: "POST",
  12. header: {
  13. "Content-Type": "application/json;charset=UTF-8",
  14. },
  15. body: {
  16. mode:'raw',
  17. raw:JSON.stringify({
  18. "mobile":mobile,
  19. "password":password,
  20. "appId":app_id
  21. })
  22. }
  23. };
  24. pm.sendRequest(loginRequest, function(err, res) {
  25. console.log("登录的 mobile: " + mobile)
  26. console.log("访问的 app_id: " + app_id)
  27. if (err) {
  28. console.log("===========> 错误信息:" + err + "<===========");
  29. } else {
  30. const jsonData = res.json();
  31. console.log("===========> loginRequest 请求响应体: " + res.text() + "<===========")
  32. pm.environment.set("access_token", jsonData.data.access_token);
  33. pm.environment.set("refresh_token", jsonData.data.refresh_token);
  34. pm.environment.set("access_token_expires",jsonData.data.expires_in);
  35. pm.environment.set("authorization",(jsonData.data.type + " " + jsonData.data.access_token));
  36. }
  37. })
  38. };
  39. function sendRefreshRequest() {
  40. const app_id = pm.environment.get("app_id");
  41. const access_token = pm.environment.get("access_token");
  42. const refresh_token = pm.environment.get("refresh_token");
  43. const refreshToken = {
  44. url: "https://www.xxx.com/aaaaa.do",
  45. method: "POST",
  46. header: {
  47. "Content-Type": "application/json;charset=UTF-8", // 注意:header 需要加上 Content-Type
  48. },
  49. body: {
  50. mode:'raw',
  51. raw:JSON.stringify({
  52. "app_id":app_id,
  53. "access_token":access_token,
  54. "refresh_token":refresh_token
  55. })
  56. }
  57. };
  58. pm.sendRequest(refreshToken, function(err, res) {
  59. // console.log("登录的 mobile: " + mobile);
  60. console.log("访问的 app_id: " + app_id);
  61. if (err) {
  62. console.log("===========> 错误信息:" + err + "<===========");
  63. } else {
  64. const jsonData = res.json();
  65. console.log("===========> refreshToken 请求的响应体: " + res.text() + "<===========")
  66. pm.environment.set("access_token", jsonData.data.access_token);
  67. pm.environment.set("refresh_token", jsonData.data.refresh_token);
  68. pm.environment.set("access_token_expires",jsonData.data.refresh_expires_in);
  69. pm.environment.set("authorization",(jsonData.data.type + " " + jsonData.data.access_token));
  70. }
  71. });
  72. }
  73. var accessToken = pm.environment.get("access_token");
  74. var refreshToken = pm.environment.get("refresh_token");
  75. var accessTokenExpires = pm.environment.get("access_token_expires");
  76. var authorization = pm.environment.get("authorization");
  77. console.log("当前的时间: " + Date.parse(new Date())/1000);
  78. console.log("返回的过期时间: " + accessTokenExpires);
  79. console.log("access_token 值: " + accessToken);
  80. console.log("refreshToken 值: " + refreshToken);
  81. console.log("authorization 值:" + authorization);
  82. // 如 ACCESS_TOKEN 没有值,则执行发送登录接口请求; ACCESS_TOKEN_EXPIRES 已过期,则执行发送刷新token的接口请求
  83. if ( !accessToken ) {
  84. console.log("没有token值,进行登录调用")
  85. sendLoginRequest();
  86. } else if (accessTokenExpires <= Date.parse(new Date())/1000) {
  87. console.log("token过期了,进行会话续期调用")
  88. sendRefreshRequest();
  89. } else {
  90. console.log("当前token有效")
  91. }

参数化传递/上下文依赖取值 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/

前置脚本

  1. pm.request.body
  2. pm.request.body.toString()
  3. pm.request.body.raw // 如果是空,则为 undefined
  4. // 转化请求体中(有占位的)数据
  5. pm.variables.replaceIn(body_raw);

后置脚本-获取本接口请求中的参数

获取本接口请求中的参数,提供给下一个接口用

  1. // 获取 Query 参数对象
  2. var queryParams = pm.request.url.query;
  3. // 获取 key 为 field1 的 query 参数的值
  4. var field1 = queryParams.get("field1");
  5. // 已键值对象方式获取所有 query 参数
  6. var quertParamsObject = queryParams.toObject();
  7. // 遍历整个 query
  8. queryParams.each((item) => {
  9. console.log(item.key); // 输出参数名
  10. console.log(item.value); // 输出参数值
  11. });
  12. // 增加 query 参数
  13. queryParams.add({
  14. key: "field1",
  15. value: "value1",
  16. });
  17. // 修改 query 参数(如不存在则新增)
  18. queryParams.upsert({
  19. key: "field2",
  20. value: "value2",
  21. });
  22. // 当 body 类型为 form-data 时,从 pm.request.body.formdata 获取请求参数
  23. var formData = pm.request.body.formdata;
  24. // 获取 key 为 field1 的 form-data 参数的值
  25. var field1 = formData.get("field1");
  26. console.log(field1); // 控制台打印 field1
  27. // 当 body 类型为 x-www-form-urlencode** 时,从 pm.request.body.urlencoded 获取请求参数
  28. var formData = pm.request.body.urlencoded;
  1. // 当 body 类型为 json 时,从 pm.request.body.raw 获取请求参数
  2. var jsonData = JSON.parse(pm.request.body.raw); // 获取请求体JSON对象
  3. title = jsonData.Name
  4. pm.environment.set('schematitle',title);
  5. console.log(schematitle); // 控制台打印参整个 json 数据
  6. }

后置脚本-获取本接口响应中的参数

  1. pm.response.json()
  2. //或 JSON.parse(responseBody)

下游接口引用上游的响应参数

image.png
image.png

URL 参数化

https://www.w3school.com.cn/js/js_string_templates.asp http://www.postmanlabs.com/postman-collection/Request.html

image.png

  1. // 环境变量中设置了 password 等值
  2. function sendLoginByPassword(){
  3. const password = pm.environment.get("password")
  4. const appId = pm.environment.get("app_id")
  5. const rsaEncrypt = {
  6. // 引用变量password
  7. url: `https://www.xxx.com.cn/tool/rsaencrypt?value=${password}`,
  8. method: "GET",
  9. header: {
  10. "Content-Type": "*/*"
  11. },
  12. };
  13. pm.sendRequest(rsaEncrypt, function(err, res) {
  14. if (err) {
  15. console.log(err)
  16. } else {
  17. const jsonData = res.json();
  18. console.log(jsonData)
  19. }
  20. })
  21. }
  22. sendLoginByPassword()