Node.js: how to consume SOAP XML web service
You don't have that many options.
You'll probably want to use one of:
- node-soap
- strong-soap (rewrite of
node-soap
) - easysoap
I think that an alternative would be to:
- use a tool such as SoapUI (http://www.soapui.org) to record input and output xml messages
- use node request (https://github.com/mikeal/request) to form input xml message to send (POST) the request to the web service (note that standard javascript templating mechanisms such as ejs (http://embeddedjs.com/) or mustache (https://github.com/janl/mustache.js) could help you here) and finally
- use an XML parser to deserialize response data to JavaScript objects
Yes, this is a rather dirty and low level approach but it should work without problems
If node-soap
doesn't work for you, just use node
request
module and then convert the xml to json if needed.
My request wasn't working with node-soap
and there is no support for that module beyond the paid support, which was beyond my resources. So i did the following:
- downloaded SoapUI on my Linux machine.
- copied the WSDL xml to a local file
curl http://192.168.0.28:10005/MainService/WindowsService?wsdl > wsdl_file.xml
- In SoapUI I went to
File > New Soap project
and uploaded mywsdl_file.xml
. - In the navigator i expanded one of the services and right clicked
the request and clicked on
Show Request Editor
.
From there I could send a request and make sure it worked and I could also use the Raw
or HTML
data to help me build an external request.
Raw from SoapUI for my request
POST http://192.168.0.28:10005/MainService/WindowsService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://Main.Service/AUserService/GetUsers"
Content-Length: 303
Host: 192.168.0.28:10005
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
XML from SoapUI
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qtre="http://Main.Service">
<soapenv:Header/>
<soapenv:Body>
<qtre:GetUsers>
<qtre:sSearchText></qtre:sSearchText>
</qtre:GetUsers>
</soapenv:Body>
</soapenv:Envelope>
I used the above to build the following node
request
:
var request = require('request');
let xml =
`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:qtre="http://Main.Service">
<soapenv:Header/>
<soapenv:Body>
<qtre:GetUsers>
<qtre:sSearchText></qtre:sSearchText>
</qtre:GetUsers>
</soapenv:Body>
</soapenv:Envelope>`
var options = {
url: 'http://192.168.0.28:10005/MainService/WindowsService?wsdl',
method: 'POST',
body: xml,
headers: {
'Content-Type':'text/xml;charset=utf-8',
'Accept-Encoding': 'gzip,deflate',
'Content-Length':xml.length,
'SOAPAction':"http://Main.Service/AUserService/GetUsers"
}
};
let callback = (error, response, body) => {
if (!error && response.statusCode == 200) {
console.log('Raw result', body);
var xml2js = require('xml2js');
var parser = new xml2js.Parser({explicitArray: false, trim: true});
parser.parseString(body, (err, result) => {
console.log('JSON result', result);
});
};
console.log('E', response.statusCode, response.statusMessage);
};
request(options, callback);