zoukankan      html  css  js  c++  java
  • Postman 接口测试配置 Pre-request Script

    本文为博主原创,转载请注明出处:

       Pre-request Script 为Postman预置脚本,用于在postman 发送请求之前执行,封装计算或获取某些请求参数。

      1. postman 脚本提供了一些默认封装好的对象和属性。

        整个请求对象为 postman 或 pm 。

        通过postman 或 pm 可以设置一些环境变量参数,可以动态获取。

      pm.environment.set(key,value);   设置环境变量
    pm.globals.unset("variable_key"); 清除全局变量
    pm.environment.unset("variable_key");  清除环境变量
    pm.globals.get("variable_key");      获取全局变量
    pm.variables.get("variable_key");    获取一个变量
    pm.environment.get("variable_key");      获取环境变量
    pm.sendRequest("https://postman-echo.com/get", function (err, response) {
        console.log(response.json());
    });  发送一个请求
    pm.globals.set("variable_key", "variable_value");  设置环境变量

      2. 通过 pre-request script 设置环境变量,并进行接口请求

      

      其中也可以添加console 打印调试日志。可以查看计算出来的参数值。

    var timestamp = Math.round(new Date().getTime()/1000);
    console.log("timestamp is "+ timestamp);
    pm.environment.set("timestamp",timestamp);
    
    // 构造一个注册请求
    const regRequest = {
       url: 'localhost:8002/user/getNonce',
       method: 'POST', 
       header: 'Content-Type: application/json', //注意要在Header中声明内容使用的类型 
       body: { 
            mode: 'raw', // 使用raw(原始)格式 
            raw: JSON.stringify({ name: '小小', password: '123456' }) //要将JSON对象转为文本发送 
            } 
    }; 
    //发送请求 
    pm.sendRequest(regRequest, function (err, res) {
        console.log("res == "+res); // 响应为JSON格式可以使用res.json()获取到JSON对象
        console.info(res);
        pm.environment.set("nonce", res.json().value)
     }); 

         上面脚本定义了一个 timestamp 的变量,并设置到环境变量中, 并将 请求 /user/getNonce 返回的值定义为nonce值,存储到环境变量中。

      如何使用预置脚本中的变量,通过 {{ }} 既可。调用如下:

    示例中 pm.sendRequest 发送请求的格式为post 请求,请求体为json 格式。如果是 post form 请求时,更改 regRequest 中德mode 既可:

      

    const regRequest = {
       url: 'localhost:8002/user/getNonce',
       method: 'POST', 
       header: 'Content-Type: application/json', //注意要在Header中声明内容使用的类型 
       body: { 
            mode: ‘urlencoded’,
            urlencoded: [{
                “key”: “grant_type”,
                “value”: “client_credentials”,
            }]
        } 
    }; 
    //发送请求 
    pm.sendRequest(regRequest, function (err, res) {
        console.log("res == "+res); // 响应为JSON格式可以使用res.json()获取到JSON对象
        console.info(res);
        pm.environment.set("nonce", res.json().value)
     }); 

     如果是get 请求:

    const url = 'http://localhost:5000/api/user/getToken/?appid=136425';
    // 发送get请求
    pm.sendRequest(url, function (err, res) { console.log(err ? err : res.text()); // 控制台打印请求文本 }); 

      

  • 相关阅读:
    java中的 equals 与 ==
    String类的内存分配
    SVN用命令行更换本地副本IP地址
    npoi 设置单元格格式
    net core 微服务框架 Viper 调用链路追踪
    打不死的小强 .net core 微服务 快速开发框架 Viper 限流
    net core 微服务 快速开发框架 Viper 初体验20201017
    Anno 框架 增加缓存、限流策略、事件总线、支持 thrift grpc 作为底层传输
    net core 微服务 快速开发框架
    Viper 微服务框架 编写一个hello world 插件02
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/14604096.html
Copyright © 2011-2022 走看看