zoukankan      html  css  js  c++  java
  • postman

    在执行一些请求时,需要先执行其他请求做为预先条件,例如请求登陆接口时,先请求获取验证码的接口,等等

    官网说明:https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts

    以下是几个例子:

    1. 发送get请求

    const regRequest = {
        url : 'https://www.abc.com/a1aa/auth/dja?_t=1614319579',
        method : 'GET'
    };
    
    // 获取验证码
    pm.sendRequest(regRequest, function (err, response) {
        pm.globals.set("global_yanzhengma", response.json().result.verifyCode);
        pm.globals.set("global_yanzhengma_id", response.json().result.verifyId);
    
        console.log(response.json());
    });

    2. 发送json格式的post请求

    data = {
        loginId: "zhangsan",
        password: "111111"
    };
    
    // 登陆
    const regRequest = {
        url : 'https://www.abc.com/aaa/1/auth/signIn',
        method : 'POST',
        header : {
            'Content-Type' : 'application/json;charset=UTF-8'
        },
        body : {
            mode : 'raw',
            raw : JSON.stringify(data)
        }
    };
    
    pm.sendRequest(regRequest, function (err, response) {
        console.log(response.json());
    });
    stringify将js对象转换为json

    3. 发送x-www-form-urlencoded格式的post请求

    //构造一个登录请求
    const loginRequest = {
        url: 'http://115.28.108.130:5000/api/user/login/',
        method: "POST",
        body: {
            mode: 'urlencoded',  // 模式为表单url编码模式
            urlencoded: 'name=张三&password=123456'
        }
    };
    
    // 发送请求
    pm.sendRequest(loginRequest, function (err, res) {
        console.log(err ? err : res.text());
    });

    4. 发送一个xml格式的post请求

    //构造请求
    const demoRequest = {
      url: 'http://httpbin.org/post',
      method: 'POST',
      header: 'Content-Type: application/xml',  // 请求头种指定内容格式
      body: {
        mode: 'raw',
        raw: '<xml>hello</xml>'  // 按文本格式发送xml
      }
    };
    
    //发送请求
    pm.sendRequest(demoRequest, function (err, res) {
      console.log(err ? err : res.json());
    });

    5. 发送文件格式的post请求

    const regRequest = {
        url : 'https://www.abc.com/aaa/1/storage?key1=value1',
        method : 'POST',
        header : [{
            key : 'token',
            value : pm.globals.get("global_token"),
            type : 'text'
        }],
        // header : {
        //     // 'Content-Type' : 'multipart/form-data',
        //     token : 'fjkdlsafjkdklajflkdajfkdjakfjdkajfkldajfkldajfkldajfkjdsalkjfdkajfdiafdsafda',
        // },   
        body : {
            mode : 'formdata',
            formdata : [{
                'key' : 'file',
                'type' : 'binary',
                'src' : '/Users/zhangyang/abc.png'
            }]
        }
    };
    
    
    pm.sendRequest(regRequest, function (err, response) {
        // global_IDCard_front_1 = response.json().result;
        console.log(response.json());
    });

    最后这个没有执行成功,有知道原因的小伙伴评论区留言哈,不胜感激

  • 相关阅读:
    windows 2008 r2 开启互访和网络发现
    uchome 模拟发布动态和通知遇到的问题
    远程无法连接win2003的mssql2000服务器
    cidaemon.exe进程占用CPU资源的解决办法
    asp.net如何生成图片验证码
    SQL Server中截取日期型字段的日期部分和时间部分
    刷新项目失败。无法从服务器中检索文件夹信息。
    CS0016: 未能写入输出文件“c:\WINDOWS\Microsoft.NET\***.dll”错误处理
    Computer Browser服务启动后自动停止
    FCK使用 体会
  • 原文地址:https://www.cnblogs.com/xiaochongc/p/14464169.html
Copyright © 2011-2022 走看看