我正在尝试使用 Postman 发送一个 GET http 请求,该请求包含一个参数,该参数是通过采用完整的请求查询字符串动态生成的(URL 中问号右侧的所有内容,在 URL 编码后),连接先前分配的共享 key ,然后对结果字符串执行 SHA-1 散列。
我会使用预请求脚本来实现这一点。
谢谢。
最佳答案
我实际上找到了解决方案并愿意分享。
var params = [
["client_id", "222"]
,["account_id", ""]
];
// Build the request body string from the Postman request.data object
var requestBody = "";
var firstpass = true;
for(var i=0;i < params.length; i++) {
if(!firstpass){
requestBody += "&";
}
requestBody += params[i][0] + "=" + params[i][1];
firstpass = false;
postman.setGlobalVariable(params[i][0], params[i][1]);
}
requestBody += postman.getEnvironmentVariable("sharedSecretKey");
postman.setGlobalVariable("requestBody", requestBody);
var mac = "";
if(requestBody){
// SHA1 hash
mac = CryptoJS.SHA1(requestBody);
}
postman.setGlobalVariable("mac", mac);
然后我只需要在 URL 中设置参数: {{baseUrl}}/get?client_id={{client_id}}&account_id={{account_id}}&mac={{mac}}
其中 {{baseUrl}} 是一个环境变量 而{{client_id}}、{{account_id}}是全局变量
希望对大家有所帮助。
谢谢。
关于javascript - 如何在 Postman 中使用 SHA-1 动态 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37333994/