使用Node.JS与SOAP api通信

使用Node.JS与SOAP api通信,node.js,soap,angular5,Node.js,Soap,Angular5,我对使用SOAP很陌生,尤其是在node中,但我真的很想学习如何使用不同的数据传输协议 我已经构建了一个Angular 5应用程序,其中包含和Express中间件以及一个Node.js服务器,用于与REST api通信。但是,现在我必须从另一个源中提取一些数据,通过SOAP进行通信。我有一个要求和身体看起来像这样: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.x

我对使用SOAP很陌生,尤其是在node中,但我真的很想学习如何使用不同的数据传输协议

我已经构建了一个Angular 5应用程序,其中包含和Express中间件以及一个Node.js服务器,用于与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>
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);
});

这不是校友
这不是一个抽象的词

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


像这样的SOAP请求通常通过哪些方式与节点通信

好吧!我想出来了。经过一段时间的测试,结果证明SOAP调用并不过于复杂。对于我的特定场景,Node js代码如下所示:

<?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>
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);
});
var请求=要求(“请求”);
var options={method:'POST',
网址:'https://server.someplace.com/MobileClient.asmx',
标题:
{“缓存控制”:“无缓存”,
“内容类型”:“text/xml”},
正文:'\r\n\r\n\r\n\r\nThisnotthereAluser\r\nThisnotthereAlpassword\r\nIdentifier.827\r\nDateST\r\n\r\n'};
请求(选项、功能(错误、响应、正文){
如果(错误)抛出新错误(错误);
控制台日志(主体);
});
我将尽可能详细地解释这一点,如果我错了,请纠正我:

这里发生了一些事情。请求是简化http调用的节点的依赖项。在options对象中,我们定义了方法,据我所知,我总是为SOAP调用发布该方法。URL是将请求定向到的URL。正文是SOAP请求的一部分,包含用于格式化数据的函数/方法和XML模式。这些函数取决于您使用的api,因此请与从中提取数据的人联系,了解他们提供了哪些方法来授予您特定需要的数据。正文还包含访问我的soap服务所需的身份验证。最后,我将结果(主体)记录在控制台中,控制台返回所有XML格式的数据

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

希望这对其他人有所帮助