PostMan Pre-request Script 生成api簽名

PostMan Pre-request Script 生成api簽名

api接口都會有簽名校驗,這個校驗在咱們api測試的時候很不方便,這裏來利用postman 前置處理來生成sig 提交。git

PostMan設置提交參數

輸入圖片說明

這裏有兩個東西咱們須要在 Pre-request Script 中預生成來填入提交數據中。github

sig:簽名串
  create_time:提交時間

##Pre-request Script 預處理,生成sigapi

var appid = 'your appid';
    var appkey = 'your appkey';
   //獲取當前時間
    function createTime() {
        return (new Date()).valueOf();
    }
    var time = createTime();
    var method = request.method;//提交方式
    delete request.data["sig"];//將sig排除排序
    console.log(request.data);
    var keys = Object.keys(request.data), i, len = keys.length;
      keys.sort();//根據key經行排序
     console.log(keys)
    // Build the request body string from the Postman request.data object
    var requestBody = "";
    var firstpass = true;
   // 構造數據爲 key=param&key=param....字符串
    for(var index in keys){
          if(!firstpass){
            requestBody += "&";
        }
        if(keys[index]=="create_time"){
            request.data[keys[index]]=time;
            console.log(request.data[keys[index]]);
        }
        requestBody += keys[index] + "=" + request.data[keys[index]];
        firstpass = false;
    }
    
      console.log(requestBody);
    //將構造數據url編碼
     var encodeURIdata = encodeURIComponent(requestBody);
     console.log(encodeURIdata);
    appkey=appkey+"&";
    //生成密鑰
    var signHmacSHA1=CryptoJS.HmacSHA1(encodeURIdata, appkey);
    console.log(signHmacSHA1);
    var base64sha256 = CryptoJS.enc.Base64.stringify(signHmacSHA1);
       console.log(base64sha256);
    //    將變量放入postman 變量中
    postman.setEnvironmentVariable('appid', appid);
    postman.setEnvironmentVariable('create_time', time);
    postman.setEnvironmentVariable('signature', base64sha256);

這樣每次提交數據就能 預先構造出sig而後提交了。app

參考:

Testing REST API with SHA1 HMAC authenticationpost

httpsig-in-postman-pre-request-script.js測試

相關文章
相關標籤/搜索