zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    OSI参考模型(转)
    H3C交换机配置常用命令(转)
    H3C交换机配置学习随笔
    [Swust OJ 247]--皇帝的新衣(组合数+Lucas定理)
    [Swust OJ 1084]--Mzx0821月赛系列之情书(双线程dp)
    [Swust OJ 404]--最小代价树(动态规划)
    [Swust OJ 610]--吉祥数
    [Swust OJ 137]--波浪数(hash+波浪数构造)
    [Swust OJ 566]--开N方数(牛顿切线法解高次方程)
    [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)
  • 原文地址:https://www.cnblogs.com/zjhblogs/p/10219346.html
Copyright © 2011-2022 走看看