设置属性
pm.globals.set("baseUrl","localhost:9900");
pm.environment.set("timeStamp",timeStamp);
秒级时间戳 也可使用postman内置变量{{$timestamp}}
var timeStamp = Math.round(new Date()/1000);
两位随机数
var nonce = Math.round(Math.random()*100);
pm.environment.set("nonce",nonce);
生成签名
var appSecret = 'appSecret';
var params = [timeStamp+"",nonce+"",appSecret];
var values = Object.values(params).sort();
var cipher = CryptoJS.SHA1(values.join("").toString()).toString();
var sign = Bytes2Str(cipher);
console.log(sign);
字节数组转16进制字符串
function Bytes2Str(arrBytes) {
var str = ""
for (var i = 0; i < arrBytes.length; i++) {
var tmp;
var num = arrBytes[i];
if (num < 0) {
tmp = (255 + num + 1).toString(16);
} else {
tmp = num.toString(16);
}
str += tmp;
}
return str;
}
获取请求体
var obj = pm.request.body.raw;
var param = JSON.parse(obj);
var des = stringToByte(values.join("").toString());
function stringToByte(str) {
var bytes = [];
for(var i = 0; i < str.length; i++) {
bytes.push(str.charCodeAt(i));
}
return bytes;
}
|