51Testing软件测试论坛
标题: PostMan Pre-request Script 生成api签名 [打印本页]
作者: 测试积点老人 时间: 2019-1-4 16:33
标题: PostMan Pre-request Script 生成api签名
PostMan Pre-request Script 生成api签名api接口都会有签名校验,这个校验在我们api测试的时候很不方便,这里来利用postman 前置处理来生成sig 提交。
PostMan设置提交参数
[attach]120656[/attach]
这里有两个东西我们需要在 Pre-request Script 中预生成来填入提交数据中。
##Pre-request Script 预处理,生成sig
- var appid = 'your appid';
- var appkey = 'your appkey';
- //获取当前时间
- function createTime() {
- return (new Date()).valueOf();
- }
- var time = createTime();
- var method = request.method;//提交方式
- delete request.data["sig"];//将sig排除排序
- console.log(request.data);
- var keys = Object.keys(request.data), i, len = keys.length;
- keys.sort();//根据key经行排序
- console.log(keys)
- // Build the request body string from the Postman request.data object
- var requestBody = "";
- var firstpass = true;
- // 构造数据为 key=param&key=param....字符串
- for(var index in keys){
- if(!firstpass){
- requestBody += "&";
- }
- if(keys[index]=="create_time"){
- request.data[keys[index]]=time;
- console.log(request.data[keys[index]]);
- }
- requestBody += keys[index] + "=" + request.data[keys[index]];
- firstpass = false;
- }
-
- console.log(requestBody);
- //将构造数据url编码
- var encodeURIdata = encodeURIComponent(requestBody);
- console.log(encodeURIdata);
- appkey=appkey+"&";
- //生成密钥
- var signHmacSHA1=CryptoJS.HmacSHA1(encodeURIdata, appkey);
- console.log(signHmacSHA1);
- var base64sha256 = CryptoJS.enc.Base64.stringify(signHmacSHA1);
- console.log(base64sha256);
- // 将变量放入postman 变量中
- postman.setEnvironmentVariable('appid', appid);
- postman.setEnvironmentVariable('create_time', time);
- postman.setEnvironmentVariable('signature', base64sha256);
复制代码
这样每次提交数据就能 预先构造出sig然后提交了。
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) |
Powered by Discuz! X3.2 |