postman Pre-request Script 发送请求

Posted wf0117

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了postman Pre-request Script 发送请求相关的知识,希望对你有一定的参考价值。

  1. 发送get请求
    var host = pm.environment.get("host"); 
    const csrfTokenServlet = {
      url: `https://${host}/{path}?csrfType=test`,
      method: ‘GET‘
    };
    
    pm.sendRequest(csrfTokenServlet, (err, response) => {
        if (err) {
            console.log(err);
        } else {
            pm.environment.set("csrfToken", response.json()[‘data‘]);
            console.log(response.json()[‘data‘])
        }
    });
  2. 发送post请求
    # content-type/x-www-form-urlencoded
    
    var host = pm.environment.get("host"); 
    
    var data = {
    "organCode":"10000065",
    }
    const postRequest = {
      url: `https://${host}/${path}`,
      method: ‘POST‘,
      header:‘Content-Type:application/x-www-form-urlencoded‘,
      body: {
        mode: ‘x-www-form-urlencoded‘,
        raw: "organCode=10000062"
      }};
    pm.sendRequest(postRequest, (err, response) => {
        if (err) {
            console.log(err);
        } else {
            console.log(response.json());
        }
    });
    
    或者
    
    var host = pm.environment.get("host"); 
    
    var data = {
    "organCode":"10000065",
    }
    const postRequest = {
      url: `https://${host}/${path}`,
      method: ‘POST‘,
      header:‘Content-Type:application/x-www-form-urlencoded‘,
      body: {
        mode: ‘urlencoded‘,
        urlencoded: "organCode=10000062"
      }};
    pm.sendRequest(postRequest, (err, response) => {
        if (err) {
            console.log(err);
        } else {
            console.log(response.json());
        }
    });
    
    # content-type/json
    var data = {
    "key1":"value1",
    "key2":"value2",}
    const echoPostRequest = {
      url: http://ip:port/XXX,
      method: ‘POST‘,
      header:‘Content-Type:application/json‘,
      body: {
        mode: ‘raw‘,
        raw: JSON.stringify(data)
      }};
    pm.sendRequest(echoPostRequest, function (err, res) {
        console.log(res.json());
    });

    参考:

    postman(六):详解在Pre-request Script中如何执行请求

    postman 在pre-request中发送application/x-www-form-urlencoded 格式表单

    postman pre-request-script 操作方法记录

     

     

以上是关于postman Pre-request Script 发送请求的主要内容,如果未能解决你的问题,请参考以下文章

Postman: Pre-request Script,设置body 变量

postman Pre-request Script 发送请求

postman之Pre-request Script分享

postman使用教程19-collection添加Pre-request Scripts 解决登录依赖token

Postman高级玩法之Pre-request Script

Postman