首页 > 解决方案 > 如何在 javascript 中发送 SOAP 请求,例如在 SoapUI 中

问题描述

我目前正在开发一个需要使用soap/xml/wsdl 的NodeJS 项目。问题是无法弄清楚这些是如何工作的,所以请原谅我的无知。这是我需要的:

我有这个 WSDL 站点,我需要从中得到一些答案。我已经想出了如何在 SoapUI 中做到这一点,但我不知道如何在 Javascript 中做到这一点。我在soapUI 中发送的请求如下所示:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:uni="https://uni-login.dk">
   <soap:Header/>
   <soap:Body>
      <uni:hentDataAftaler>
         <uni:wsBrugerid>?</uni:wsBrugerid>
         <uni:wsPassword>?</uni:wsPassword>
      </uni:hentDataAftaler>
   </soap:Body>
</soap:Envelope>

我也有 wsdl 链接: https ://wsiautor.uni-login.dk/wsiautor-v4/ws?WSDL

我也尝试在 nodeJS 中使用一些 npm 包(SOAP、Strong-SOAP 和 Easy-SOAP),但我也无法使这些工作。

我希望你有一些建议,如果你需要更多信息来回答我的问题,请告诉我:)

标签: javascripthtmlnode.jsxmlsoap

解决方案


您可以使用easy-soap-request,这篇文章https://medium.com/@caleblemoine/how-to-perform-soap-requests-with-node-js-4a9627070eb6可能会有所帮助。它只是 axios 的一个薄包装。

我的问题代码:

const soapRequest = require('easy-soap-request');
const url = 'https://wsiautor.uni-login.dk/wsiautor-v4/ws';
const headers = {
    'Content-Type': 'application/soap+xml;charset=UTF-8',
    'soapAction': 'https://wsiautor.uni-login.dk/hentDataAftaler',
};
// example data
const xml = `
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:uni="https://uni-login.dk">
   <soap:Header/>
   <soap:Body>
      <uni:hentDataAftaler>
         <uni:wsBrugerid>?</uni:wsBrugerid>
         <uni:wsPassword>?</uni:wsPassword>
      </uni:hentDataAftaler>
   </soap:Body>
</soap:Envelope>
`;


// usage of module
soapRequest(url, headers, xml).then(({response: {body, statusCode}}) => {
    console.log(body);
    console.log(statusCode);
}).catch((errorBody) => {
    console.error(errorBody);
});

推荐阅读