乐心
Always dream. Always explore.
乐心的博客
Postman Pre-request Script

提供一份样例,供参考

var apikey = 'key';
// 获取当前时间
var timestamp = parseInt(pm.variables.replaceIn('{{$timestamp}}'));
var salt = 'secret';
var expires = String(timestamp + 3600);
// 获取请求路径
var path = pm.request.url.getPath();
// 生成签名,需要utf8字符集
var signature = CryptoJS.enc.Utf8.parse(path);
var secret = CryptoJS.enc.Utf8.parse(salt + expires);
var hmac = CryptoJS.HmacSHA256(signature, secret).toString();
// 设置到变量
pm.variables.set('hmac', hmac);
pm.variables.set('expires', expires);
// 追加到query参数
pm.request.addQueryParams('APIKEY=' + apikey);
pm.request.addQueryParams('EXPIRES=' + expires);
pm.request.addQueryParams('HMAC=' + hmac);

参考sdk:

Postman Collection: https://www.postmanlabs.com/postman-collection/index.html

如果喜欢这篇文章,欢迎推荐给你的朋友

发表评论

textsms
account_circle
email

乐心的博客

Postman Pre-request Script
提供一份样例,供参考 var apikey = 'key'; // 获取当前时间 var timestamp = parseInt(pm.variables.replaceIn('{{$timestamp}}')); var salt = 'secret'; var expires = String(time…
扫描二维码继续阅读
2020-09-24