PostMan Pre-request Script 生成api签名
PostMan Pre-request Script 生成api签名api接口都会有签名校验,这个校验在我们api测试的时候很不方便,这里来利用postman 前置处理来生成sig 提交。PostMan设置提交参数这里有两个东西我们需要在 Pre-request Script 中预生成来填入提交数据中。
sig:签名串
create_time:提交时间
##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=="create_time"){
request.data]=time;
console.log(request.data]);
}
requestBody += keys + "=" + request.data];
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然后提交了。
页:
[1]