简体   繁体   English

如何在 javascript 中发送 SOAP 请求,就像在 SoapUI 中一样

[英]How to send a SOAP request in javascript, like in SoapUI

I am currently working on a NodeJS project where I need to use some soap/xml/wsdl.我目前正在开发一个 NodeJS 项目,我需要在其中使用一些 soap/xml/wsdl。 The problem is that can't figure out how any of these works, so forgive my ignorance.问题是无法弄清楚这些是如何工作的,所以请原谅我的无知。 Here is what I need:这是我需要的:

I have this WSDL site that I need to get some answers from.我有这个 WSDL 站点,我需要从中获得一些答案。 I have figured out how to do this in SoapUI, but I have no idea how to do it in Javascript.我已经想出了如何在 SoapUI 中做到这一点,但我不知道如何在 Javascript 中做到这一点。 The request that I am sending in soapUI looks like this:我在 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>

I also have the wsdl-link: https://wsiautor.uni-login.dk/wsiautor-v4/ws?WSDL我也有 wsdl 链接: https : //wsiautor.uni-login.dk/wsiautor-v4/ws?WSDL

I have also tried to use some npm-packages in nodeJS (SOAP, Strong-SOAP and Easy-SOAP), but I can't make these work either.我还尝试在 nodeJS(SOAP、Strong-SOAP 和 Easy-SOAP)中使用一些 npm-packages,但我也无法使这些工作。

I hope you I have some suggestions and tell me if you need any more information to answer my question :)我希望你有一些建议,如果你需要更多信息来回答我的问题,请告诉我:)

You can use easy-soap-request ,and this article https://medium.com/@caleblemoine/how-to-perform-soap-requests-with-node-js-4a9627070eb6 may help.您可以使用easy-soap-request ,这篇文章https://medium.com/@caleblemoine/how-to-perform-soap-requests-with-node-js-4a9627070eb6可能会有所帮助。 It is just a thin wrapper of axios.它只是 axios 的一个薄包装。

My code for your question:我的问题代码:

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);
});

将来可能对某人有用:将此(url, headers, xml)更改为( {url, headers, xml})

for those who get an error after following @aristoll answer.对于那些在@aristoll 回答后遇到错误的人。 Try this, its gonna work试试这个,它会工作

before

soapRequest(url, headers, xml).....

after

soapRequest({url, headers, xml})......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM