快速发送带有模拟表单数据的POST请求

时间:2019-08-01 06:38:17

标签: node.js express https

我将我的Express设置为一种代理,每当我打/ speise时,我都希望Express对其中带有表单数据的某个网站发出POST请求。这是我要表达的要求: postman request

我尝试使用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();

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

1 个答案:

答案 0 :(得分:0)

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

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'})
  })
});