postman pre-request-script 操作方法記錄


 上代碼----自己參考下就明白了

例子1:自動登陸獲取token

let chatHost,chatName,chatPassword;
//設置環境變量
if (pm.environment.get('localhost.chat') === undefined) { pm.environment.set("localhost.chat", 'localhost:3000'); pm.environment.set("chat.name", 'yourname'); pm.environment.set("chat.password", 'yourpassword'); } chatHost = pm.environment.get('localhost.chat'); chatName = pm.environment.get('chat.name'); chatPassword = pm.environment.get('chat.password'); //編輯請求內容 const echoPostRequest = { url: `${chatHost}/api/v1/login`, method: 'POST', header:'Content-Type:application/x-www-form-urlencoded', body: { mode: 'x-www-form-urlencoded', raw: `user=${chatName}&password=${chatPassword}` } };
//發起請求獲取token pm.sendRequest(echoPostRequest,
function (err, response) { console.log(response.json(response)); let res = response.json(response); pm.environment.set("chat.authToken",res.data.authToken); pm.environment.set("chat.userId",res.data.userId); });

 

 

例子2: 自動簽名

const wdsign = {
    setEnvironment: function() {
        if (pm.environment.get('ClientId') === undefined) {
            pm.environment.set("ClientId", 'client');
        }
        if (pm.environment.get('Secret') === undefined) {
             pm.environment.set("Secret", '簽名秘葯');
        }
        pm.environment.set("RequestTime", this.getRequestTime());
    },
    getRequestTime: function() {
        return parseInt(new Date().getTime() / 1000);
    },
    getSign: function() {
        let data = request.data;
        let dataString = '';
        
        if (typeof data === 'object') {
            Object.keys(data).sort().forEach(function(ele, index) {
                let value = data[ele]
   
                dataString += ele + '=' + value + '&';
            })
        }
        dataString = dataString.substr( 0,dataString.length-1 );
        dataString += pm.environment.get('Secret');
        console.log(dataString);
        return CryptoJS.MD5(dataString).toString();
    },
    
    run: function() {
        this.setEnvironment();
        pm.environment.set("Sign", this.getSign());
    }
}

wdsign.run();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM