【问题标题】:Communicating with SOAP api using Node.JS使用 Node.JS 与 SOAP api 通信
【发布时间】:2018-06-10 21:14:39
【问题描述】:

我是使用 SOAP 的新手——尤其是在节点内,但我真的很想学习如何使用不同的数据传输协议。

我已经使用 Express 中间件和 Node.js 服务器构建了一个 Angular 5 应用程序来与 REST api 通信。但是,现在我必须从不同的来源提取一些数据,通过 SOAP 进行通信。我的请求和正文如下所示:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetDashboardInfo xmlns ="http://IAmAURL.com/client">
<username>ThisIsNotTheRealUser</username>
<password>ThisIsNotTheRealPassword</password>
<applicationIdentifier>Identifier.827</applicationIdentifier>
<deviceIdentifier>DataTest</deviceIdentifier>
</GetDashboardInfo>
</soap:Body>
</soap:Envelope>

向客户端发送 POST:https://server.someplace.com/MobileClient.asmx

我知道在使用 PostMan 时请求会返回我想要的数据:

通常有哪些方法可以将这样的 SOAP 请求与 Node 进行通信?

【问题讨论】:

    标签: node.js soap angular5


    【解决方案1】:

    好吧!我想到了。经过一段时间的测试,发现 SOAP 调用并不过分复杂。对于我的特定场景,Node js 代码如下所示:

    var request = require("request");
    
    var options = { method: 'POST',
      url: 'https://server.someplace.com/MobileClient.asmx',
      headers: 
       { 'Cache-Control': 'no-cache',
         'Content-Type': 'text/xml' },
      body: '<?xml version="1.0" encoding="utf-8"?>\r\n<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">\r\n<soap:Body>\r\n<GetDashboardInfo xmlns ="http://IAmAURL.com/client">\r\n<username>ThisIsNotTheRealUser</username>\r\n<password>ThisIsNotTheRealPassword</password>\r\n<applicationIdentifier>Identifier.827</applicationIdentifier>\r\n<deviceIdentifier>DataTest</deviceIdentifier>\r\n</GetDashboardInfo>\r\n</soap:Body>\r\n</soap:Envelope>' };
    
    request(options, function (error, response, body) {
      if (error) throw new Error(error);
    
      console.log(body);
    });
    

    我会尽量详细解释,如果我错了,请纠正我:

    这里发生了一些事情。 request 是 node 的依赖项,它简化了 http 调用。在选项对象中,我们定义了方法,据我所知,我总是 POST 进行 SOAP 调用。 URL 是指向您的请求的 URL。 Body 是 SOAP 请求的一部分,包含用于格式化数据的函数/方法和 XML 模式。这些功能取决于您使用的 api,因此请检查您从谁那里提取数据,他们提供哪些方法来授予您特别需要的数据。 Body 还包含访问我的 soap 服务所需的身份验证。最后,我在控制台中记录结果(正文),该控制台返回所有以 XML 格式格式化的数据。

    太棒了!现在怎么办?那么现在我需要将 XML 格式化为我可以实际使用的 JSON 对象。为此,我可能会使用节点包 xml2json ,但这是一个单独的问题。

    希望这对某个地方的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-10
      • 2016-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多