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 操作方法记录

     

相关推荐