【测试】-前置脚本-给请求的接口 URL 添加参数

2023年11月16日 18:14 · 阅读(2029) ·

前置脚本

  1. //获取环境变量
  2. var appid = pm.variables.get("appid")
  3. var appkey = pm.variables.get("appkey");
  4. var base_url = pm.variables.get("base_url");
  5. var hmac = pm.variables.get("hmac");
  6. //1. 获取环境变量中秘钥值.不同环境请输入不同的环境变量值
  7. console.log("appid:" + appid + ",appkey:" + appkey)
  8. //2. 准备请求参数
  9. var timestamp = pm.variables.replaceIn('{{$timestamp}}');
  10. console.log("timestamp:" + timestamp)
  11. //3. nonce 简化nonce值
  12. // var nonce=pm.variables.replaceIn('{{$guid}}')
  13. var nonce = createUuid();
  14. console.log("nonce:" + nonce)
  15. //2. 计算body md5值 作为payload 如果是设置了签名算法为hmac则不计算body payload_sigm
  16. // 默认为hmac-with-body 则需要计算body
  17. var payload_sig = ""
  18. console.log("hmac:" + hmac);
  19. if(!hmac){
  20. var payload_sig = calcMd5();
  21. console.log("payload_sig:" + payload_sig);
  22. }
  23. // 加入所有请求参数,进行计算signature
  24. paramsSort = paramsSort();
  25. console.log("paramsSort:" + paramsSort);
  26. tempsignature = paramsSort + "&key=" + appkey;
  27. var signature = CryptoJS.HmacSHA256(tempsignature, appkey).toString().toUpperCase()
  28. console.log("signature:" + signature)
  29. queryString = paramsSort + "&signature=" + signature;
  30. console.log("queryString:" + queryString)
  31. //URL 添加参数
  32. pm.request.addQueryParams("appid=" + appid);
  33. pm.request.addQueryParams("timestamp=" + timestamp);
  34. pm.request.addQueryParams("nonce=" + nonce);
  35. if (payload_sig) {
  36. pm.request.addQueryParams("payload_sig=" + payload_sig);
  37. }
  38. pm.request.addQueryParams("signature=" + signature);
  39. //发起请求 debug 脚本使用
  40. var fullUrl = request.url.split('?')[0] + "?" + queryString;
  41. fullUrl = fullUrl.replace('{{base_url}}', base_url);
  42. console.log("fullUrl:" + fullUrl)
  43. // const postRequest = {
  44. // url: fullUrl,
  45. // method: "POST",
  46. // body: pm.request.body.raw
  47. // };
  48. // pm.sendRequest(postRequest, function (err, response) {
  49. // console.log(response.json());
  50. // });
  51. // 请求参数排序
  52. function paramsSort() {
  53. var params = new Map();
  54. var contentType = pm.request.headers.get("content-type");
  55. if (contentType && contentType.startsWith('application/x-www-form-urlencoded')) {
  56. const formParams = request.data.split("&");
  57. formParams.forEach((p) => {
  58. const ss = p.split('=');
  59. params.set(ss[0], encodeRFC2396URI(ss[1]));
  60. })
  61. }
  62. const ss = request.url.split('?');
  63. if (ss.length > 1 && ss[1]) {
  64. const queryParams = ss[1].split('&');
  65. queryParams.forEach((p) => {
  66. const ss = p.split('=');
  67. params.set(ss[0], encodeRFC2396URI(ss[1]));
  68. })
  69. }
  70. params.set("appid", appid);
  71. params.set("timestamp", timestamp);
  72. params.set("nonce", nonce);
  73. if (payload_sig) {
  74. params.set("payload_sig", payload_sig);
  75. }
  76. var sortedKeys = Array.from(params.keys())
  77. sortedKeys.sort(function (a, b) {
  78. return a.toLowerCase().localeCompare(b.toLowerCase());
  79. });
  80. var qs
  81. for (var k of sortedKeys) {
  82. var s = k + "=" + params.get(k);
  83. qs = qs ? qs + "&" + s : s;
  84. // console.log("key=" + k + " value=" + params.get(k));
  85. }
  86. return qs;
  87. }
  88. // 计算md5 body的请求值
  89. function calcMd5() {
  90. pm.request.addHeader("Content-Type:application/json");
  91. var contentType = pm.request.headers.get("Content-Type");
  92. var data = pm.request.body.raw;
  93. if (data && !contentType.startsWith('application/x-www-form-urlencoded')) {
  94. console.log("request.data:" + data);
  95. var md5 = CryptoJS.MD5(data).toString().toUpperCase();
  96. return md5;
  97. } else {
  98. return "";
  99. }
  100. }
  101. // 随机ID 生成算法
  102. function createUuid() {
  103. return 'xxxyxxx'.replace(/[xy]/g, function (c) {
  104. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  105. return v.toString(16);
  106. });
  107. }
  108. //java js RFC2396实现
  109. // https://www.cnblogs.com/linyufeng/p/14676964.html
  110. // 由于spring 框架会默认decode 一次
  111. function encodeRFC2396URI(str) {
  112. return encodeURIComponent(decodeURIComponent(str))
  113. .replace(/%20/g, "+")
  114. .replace(/\)/g, "%29")
  115. .replace(/\(/g, "%28")
  116. .replace(/'/g, "%27")
  117. .replace(/!/g, "%21")
  118. .replace(/~/g, "%7E");
  119. }

测试

  • 请求数据 {{baseurl}}/smallflowService/1.0/getAccountLicenceData

  • 实际请求

  1. {{baseurl}}/smallflowService/1.0/getAccountLicenceData?appid=1111
  2. &timestamp=1700128978
  3. &nonce=222
  4. &payload_sig=3333
  5. &signature=444