欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

postman pre-request script 替换url变量

程序员文章站 2022-07-12 11:27:43
...

原文地址:http://www.nanstar.top/p/wiki_1576118926436

场景

在添加验签字段时,需要在pre-request script中执行。如果请求参数中包含变量,如 ?userid={{userid}} 在pre-request script中获取url时仍然是userid={{userid}}的形式,并没有使用实际值替换掉。

期望

变量应该被替换,如 ?userid=123456

解决方法

没有找到具体方法,github上也没有现成的代码,有人推荐使用正则替换,先记下正则的用法,以后备用。

var regex1 = RegExp('foo*','g');
var str1 = 'table football, foosball';
var array1;

while ((array1 = regex1.exec(str1)) !== null) { 
  console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
  // expected output: "Found foo. Next starts at 9."
  // expected output: "Found foo. Next starts at 19."
}

更新 20191213

找到了一个有类似功能的代码:https://gitee.com/qingtianbuluoye/Postman-encryption
该代码的encryption.js 中有替换变量的方法。不过代码好长啊!!

原文地址:http://www.nanstar.top/p/wiki_1576118926436