zoukankan      html  css  js  c++  java
  • postman高级用法之pre-request

    一、pre-request

    1.简单用法
    //获取、设置、删除环境变量
    pm.environment.get("variable_key");
    pm.environment.set("variable_key", "variable_value");
    pm.environment.unset("variable_key");
    //获取设定删除全局变量
    pm.globals.get("variable_key");
    pm.globals.set("variable_key", "variable_value");
    pm.globals.unset("variable_key");
    //获取普通变量
    pm.variables.get("variable_key");
    //获取设置、删除集合变量
    pm.collectionVariables.get("variable_key");
    pm.collectionVariables.set("variable_key", "variable_value");
    pm.collectionVariables.unset("variable_key");
    ​
    //发送一个get请求
    pm.sendRequest("https://postman-echo.com/get", function (err, response) {
        console.log(response.json());
    });
    2.发送前置post请求,从中获取cookie,然后应用到第二个请求的header,常用于鉴权
    const loginRequest={
        url:"https://www.xxx.com/api/user/login",
        method:"POST",
         header: [
          "Content-Type: application/x-www-form-urlencoded; charset=UTF-8",
         "appVer: 4.4.0",
          ],
      body: {
        mode: 'raw',
        raw: 'loginName=user&loginPwd=pswd'
      }
    };
    pm.sendRequest(loginRequest,function(err,response){
        r = JSON.parse(JSON.stringify(response));
        for(var x of r.header){
            if(x.key=="Set-Cookie"){
                console.log(x.value);
                pm.collectionVariables.set("cookie",x.value);
            }
        }
      
    });

     

  • 相关阅读:
    左侧列固定的表格
    百度地图上添加多个标记,标记上添加信息展示窗口、跳转到导航界面
    vue-cli4版本解决eslint问题
    使用脚手架搭建项目
    正则表达式学习
    函数参数:
    装饰器(重点)
    列表生成式、生成器、迭代器
    logging 日志模块
    json 、 pickle 、shelve序列化
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15151546.html
Copyright © 2011-2022 走看看