postman 變量能夠這樣寫:{{timestamp}} ,也能夠用系統的,{{$timestamp}},這樣就不用給本身賦值了,但在 pre-requestScript中是獲取不到這個值的後端
因此我這用自定義變量:app
而後在postman中定義這些變量dom
而後就是在pre-requestScript中寫js腳本賦值和獲取值,而後MD5加密post到後端了post
// 獲取全局變量 appid = pm.environment.get("appid"); appkey = pm.environment.get("appkey"); //設置時間戳 pm.environment.set("timestamp",Math.round(new Date().getTime())); timestamp = pm.environment.get("timestamp"); //設置GUID pm.environment.set("guid",newGuid()); guid = pm.environment.get("guid"); console.log(newGuid()); var arr = new Array(4) arr[0] = appid arr[1] = guid arr[2] = appkey arr[3] = timestamp var abc = arr.sort(); //排序 //console.log(arr); //console.log(abc); //MD5加密 var md5= CryptoJS.MD5(abc.join("")).toString(); pm.environment.set('signature',md5); /* 生成GUID */ function newGuid() { var guid = ""; for (var i = 1; i <= 32; i++){ var n = Math.floor(Math.random()*16.0).toString(16); guid += n; if((i==8)||(i==12)||(i==16)||(i==20)) guid += "-"; } return guid; }
能夠console.log輸入日誌看看,pre-requestScript中獲取的值。並加密成signature 的appid,appkey 等傳到後端的值是否一致測試
測試結果一致:ui