Postman 随机数
2021-09-22 本文已影响0人
乘风破浪的姐姐
Postman内置自动生成的随机数的参数
{{$guid}}:添加一个V4风格GUID(如: aa002-44ac-45ca-aae3-52bf19650e2d)
{{$timestamp}}:将当前的时间戳,精确到秒
{{$randomInt}}:添加0和1000之间的随机整数
Postman的pre-requestScript中,自定义随机数
//产生随机数字
function GetRandomNum(Min,Max)
{
var Range = Max - Min;
var Rand = Math.random();
return(Min + Math.round(Rand * Range));
}
//产生随机字符串
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}
var tid = GetRandomNum(1111111111,99999999999);
var num = GetRandomNum(25,35);
var oaid = randomString(num, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
console.log("oaid is : "+oaid);
console.log("tid is : "+tid);
//设置环境变量
pm.environment.set("oaid", oaid);
pm.environment.set("tid", tid);
Postman获取当前时间
var moment = require("moment"); // 获取时间
var data = moment().format(" YYYY-MM-DD HH:mm:ss"); //定义时间格式
console.log(data);
pm.globals.set("TIME", data); //设置为全局变量
Postman获取当前时间戳(毫秒)
//设置当前时间戳(毫秒)
Timestamp = Math.round(new Date().getTime());
postman.setGlobalVariable("Timestamp",Timestamp);
Postman签名
//设置签名秘钥
key = "E84F708A9B8B42E6A08F9025CBBCC934";
//字符串进行md5加密
var token=pm.request.headers.get("Token")||"";
var body=pm.request.body.raw;
body = body.replace("{{Timestamp}}",Timestamp);
//计算签名
var str = token+"&"+body+"&"+key;
postman.setGlobalVariable("str",str);
var strmd5= CryptoJS.MD5(str).toString(CryptoJS.enc.Hex).toUpperCase();
postman.setGlobalVariable("SignKey",strmd5);