2016-05-19 56 views
2

我試圖使用Postman發送一個GET http請求,其中包含一個參數,該參數是通過將完整的請求查詢字符串(問號右側的所有內容在URL中,在URL編碼之後),連接先前分配的共享密鑰,然後執行結果字符串的SHA-1散列。如何在Postman中使用SHA-1動態密鑰

我會使用預先請求腳本來實現這一點。

謝謝。

回答

4

我確實找到了解決方案並希望分享它。

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}} /獲得的client_id = {{CLIENT_ID}} & ACCOUNT_ID = {{ACCOUNT_ID}} & MAC = {{MAC}}

其中{{的baseUrl}}是一個環境變量 和{{CLIENT_ID}},{{ACCOUNT_ID}}是全局變量

希望對某人有幫助。

謝謝。

+0

正是我在找的東西。謝謝! :) – Loaf