尝试通过节点调用国家铁路 SOAP API 时出现类型错误

TypeError when trying to call National Rail SOAP API via Node

我正在尝试使用 node-soap 访问国家铁路 API。

文档在这里:https://lite.realtime.nationalrail.co.uk/OpenLDBWS/

我是 SOAP 的新手,所以这可能不是 API 的问题。无论我尝试什么,我都会收到 TypeError...

TypeError: Cannot read property 'Body' of undefined

这是我用来尝试拨打电话的代码:

var soap = require('soap');
var url = 'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2014-02-20';
var soapHeader = '<com:AccessToken>MY_TOKEN</com:AccessToken>';

soap.createClient(url, function(err, client){

  var args = {
    numRows : 10,
    crs : 'LAN'
  };

  client.addSoapHeader(soapHeader);

  client.GetDepartureBoard(args, function(err, result){
    if(err){
      console.log('error!');
      throw err;
    }
    console.log(result);
  });  
});

使用node-soap我也可以得到请求信封,这是内容:

<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tns="http://thalesgroup.com/RTTI/2014-02-20/ldb/"
    xmlns:tok="http://thalesgroup.com/RTTI/2013-11-28/Token/types"
    xmlns:ct="http://thalesgroup.com/RTTI/2007-10-10/ldb/commontypes"
    xmlns:ldbt="http://thalesgroup.com/RTTI/2012-01-13/ldb/types"
    xmlns:ldbt2="http://thalesgroup.com/RTTI/2014-02-20/ldb/types">
    <soap:Header>
        <com:AccessToken>MY_TOKEN</com:AccessToken>
    </soap:Header>
    <soap:Body>
        <tns:GetDepartureBoardRequest
            xmlns:tns="http://thalesgroup.com/RTTI/2014-02-20/ldb/"
            xmlns="http://thalesgroup.com/RTTI/2014-02-20/ldb/">
            <tns:numRows>10</tns:numRows>
            <tns:crs>LAN</tns:crs>
        </tns:GetDepartureBoardRequest>
    </soap:Body>
</soap:Envelope>

您的 soap header 格式不正确,需要 '<'AccessToken'>''<'/AccessToken'>' 缠绕着它

var args, soap, soapHeader, url;

soap = require('soap');

url = 'https://lite.realtime.nationalrail.co.uk/OpenLDBWS/wsdl.aspx?ver=2014-02-20';

soapHeader = '<AccessToken><TokenValue>Your access token</TokenValue></AccessToken>'

// or you can write it as an object
//soapHeader = {
//    AccessToken: {
//        TokenValue: "Your access token"
//    }
//};

args = {
    numRows: 10,
    crs: 'BSK'
};

soap.createClient(url, function(err, client) {
    client.addSoapHeader(soapHeader);
    return client.GetArrivalDepartureBoard(args, function(err, result) {
        return console.log(JSON.stringify(result));
    });
});