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);
            }
        }
      
    });

     

  • 相关阅读:
    HolidayFileDisPersonViewList.js中的一些基础
    保存会话数据的两种技术,Cookie,Session
    web服务器调用Servlet的过程
    XML基础
    HttpServletRequest,HttpServletResponse
    金蝶handler中 collection 代码片段理解
    Rancher 容器云平台搭建
    Docker的安装及镜像加速配置
    MYSQL 合并行
    Spring Ioc 容器核心概览
  • 原文地址:https://www.cnblogs.com/wangbin2188/p/15151546.html
Copyright © 2011-2022 走看看