接口测试 Postman 使用脚本动态生成签名参数

leayn for 贝壳找房 · December 27, 2018 · Last by leayn replied at January 07, 2019 · 2709 hits

时间戳使用

时间戳是接口请求非常常用参数,有秒和毫秒两种常用单位

#获取毫秒级别
postman.setGlobalVariable(timestamp,Math.round(new Date().getTime()));

#获取秒级别
postman.setGlobalVariable("timestamps",Math.floor(new Date().getTime()/1000));`

🎁 ** 总结 ** :脚本其实使用的是 Js,如果需要其他模式可以直接搜索 js 代码,更容易获取答案。

md5 加密

接口的 sign 很多情况是通过集中参数拼接,后 MD5 加密后得到。
举例说明:

签名 sign 值=时间戳 +param1+param2+token 拼接后 md5 加密

#获取时间戳
postman.setGlobalVariable("timestamps",Math.floor(new Date().getTime()/1000));
timestamps = postman.getGlobalVariable('timestamps');
var param1 = "testparm1";
var param2 = "testparm2"
var token = "AJ3Z_dsafdsaMVAsssdsfdsad7XdddxudjiCYiC";
var str2 = timestamps + param1 + param2 + token;

postman.setGlobalVariable("str2",str2)
var xstrmd5 = CryptoJS.MD5(str2).toString().toUpperCase();
postman.setGlobalVariable("xstrmd5",xstrmd5);`

在 postman 中使用

如下图说明:

参数 body
{{timestamps}} 就是引用脚本中的时间戳参数 timestamps
{{xstrmd5}} 就是引用脚本中计算的签名 xstrmd5

上面脚本写入 ** Pre-request Script **

共收到 2 条回复 时间 点赞

很多人在吐槽 postman 简单的同时,遗忘了 pre-scripts 和 tests 两个模块的强大之处,简单来说,pre-scripts 准备测试数据,test 获取动态参数和校验参数,我挺喜欢 postman 的使用。

leayn #2 · January 07, 2019 Author
Test44 回复

是的,工具只是一个载体,postman 已经很强大

xinxi postman 接口测试 中提及了此贴 10 Apr 21:45
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up