【问题标题】:Sending a POST request with mocked form-data in express以快递方式发送带有模拟表单数据的 POST 请求
【发布时间】:2019-08-01 14:51:17
【问题描述】:

我将我的 express 设置为一种代理,每当我点击 /speise 时,我都希望 express 向某个网站发出 POST 请求,其中包含表单数据。这是我想以快递方式执行的请求:

我尝试使用 axios 和 https.request 来执行此操作,但我不知道如何解析请求正文中的表单数据。 这是我尝试使用 axios 的方法:

axios({
        method: 'post',
        url: 'https://www.viennafood.at/speise',
        headers: {
            'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
            'etlap_post': 'etlap_post',
            'week_31': '31. Woche',
            'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
        },
        data: {
            'etlap_post': 'etlap_post',
            'week_31': '31. Woche',
            'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
        }
    }).then(function (response) {
        console.log(new Date(), 'Callback from request to viennafood');
        body = response.data;
        res.set('Content-Type', 'text/html')
        res.send(body);
        console.log(new Date(), 'Sent response to client');
        next();
    }).catch(function (error) {
        console.log(error);
    });

这是使用 https.request 的方法:

var options = {
        hostname: HTTPS_HOST,
        port: HTTPS_POST,
        path: API_ENDPOINT,
        method: HTTP_METHOD,
        headers: {
            'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
            'etlap_post': 'etlap_post',
            'week_31': '31. Woche',
            'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
        }
    }, body = '';

    console.log(new Date(), 'Sending request to viennafood');

    var requ = https.request(options, function(https_res) {
        console.log(new Date(), 'Callback from request to viennafood');

        /*
        * For future use, if the cookie gets invalid 
        */
        if (https_res.headers['set-cookie']) {
          let sessionId = https_res.headers['set-cookie'].toString().split('=')[1].split(';')[0];
        }

        https_res.on('data', function(d) {
          body += d;
        });

        https_res.on('end', function() {
            res.send(body);
            console.log(new Date(), 'Sent response to client');
            next();
        });
    });
    requ.end();

他们都没有执行与邮递员相同的请求,希望你们中的某个人能帮助我!

【问题讨论】:

    标签: node.js express https


    【解决方案1】:

    尝试按照documentation 中的建议使用querystring.stringify 序列化请求负载:

    const querystring = require('querystring');
    
    axios({
      method: 'post',
      url: 'https://www.viennafood.at/speise',
      headers: {
        'Cookie': 'PHPSESSID=u1bdc2pgjuft7biqrbd9ea43c5',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
        'etlap_post': 'etlap_post',
        'week_31': '31. Woche',
        'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
      },
      data: querystring.stringify({
        'etlap_post': 'etlap_post',
        'week_31': '31. Woche',
        'body': JSON.stringify({'etlap_post': 'etlap_post', 'week_31': '31. Woche'})
      })
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 2020-11-15
      • 2020-06-05
      • 2015-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多