项目场景:
使用Postman模拟发送请求以测试接口,要求postman读取本地文件,并对文件进行签名
问题描述
Pre-request Script中无法获取Body中的文件
...
console.log(request)
...
原因分析:
这是由于Postman出于安全考虑而将javascript脚本运行在沙盒中,而沙盒无法访问本地文件
参考:
https://www.jianshu.com/p/a75116c9bdb2
https://community.postman.com/t/how-can-i-read-a-json-file-from-within-a-pre-request-script-without-using-the-runner/25835
https://community.postman.com/t/how-to-parse-a-local-json-xml-file-in-a-postman/5520/2
解决方案:
通过在pre-request script中构建请求来获取本地文件即可
首先,使用http-server将本地文件映射到API中
>>> npm install http-server -g
>>> http-server
参考:https://stackoverflow.com/a/16350826
...
pm.sendRequest({
url: `http://127.0.0.1:8080/Book1.xlsx`,
method: 'GET',
}, function(err, res) {
console.log(res.text())
console.log(CryptoJS.SHA1(res.text()).toString())
});
...
但是问题出现了:将此方式获取的文件进行SHA1编码与在线sha1以及Python sha1编码结果不一致
查了好久没有发现是什么原因,如果你知道,望不吝赐教🙏