postman Pre-request Script 发送请求

非 Y 不嫁゛ 提交于 2021-01-06 15:46:24
  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 操作方法记录

     

     

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!