通过Node.js和Firebase函数发送xml Rest请求时发生问题

时间:2019-01-20 22:12:02

标签: node.js firebase google-cloud-functions mindbody

Node.js的新手,如果有人可以指导我在哪里犯错或错过,我将不胜感激。

我正在制作一个fireBase函数,它在该函数上调用外部API。 当运行firebase函数时,出现以下错误。

我了解这是SOAP服务,但是当通过SOAPUI,Postman和Swift 4.0与REST遇到相同的请求时,我得到了有效的响应。只有node.JS出现问题

<html>
   <head>
      <meta content="HTML Tidy for Java (vers. 26 Sep 2004), see www.w3.org" name="generator"/>
      <title>Bad Request</title>
   </head>
   <body>
      <h1>Bad Request</h1>
      Your browser sent a request that this server could not understand.
      <p>Reference #7.5c6775c7.1548021657.449091a5</p>
   </body>
</html>

exports.fetchClients = functions.https.onRequest((request, response) => {
    var https = require('https');
    var postData = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://clients.mindbodyonline.com/api/0_5_1"><soapenv:Header/><soapenv:Body><GetClients><Request>>........</Request></GetClients></soapenv:Body></soapenv:Envelope>`;

    const options = {
        protocol:"https:",
        hostname: 'api.mindbodyonline.com',
        path: '/0_5_1/ClientService.asmx',
        method: "POST",
        headers: {
            "Content_Type": "text/xml",
            "SOAPAction": "....",
            "API-key": "....",
            "SiteId": "...",
            "Connection":"Keep-Alive",
            "Host":"api.mindbodyonline.com",
            "Accept-Encoding":"Accept-Encoding: gzip,deflate",
        }
    };

    var req = https.request(options, function(res) {
        var responseData = "";
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            responseData += chunk;
        });

        res.on('end', function (chunk) {
            console.log("responseData: " + responseData);
            response.send(responseData);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
        response.send(e);
    });

    req.write(postData);
    req.end();
});

更新! 内容类型标头名称上有一个错字。这就是为什么请求没有通过的原因。现在正在工作:)

之前:Content_Type 之后:Content-Type

0 个答案:

没有答案