I successfully used "soap" package (https://www.npmjs.com/package/soap) on more than 10 tracking WebApis (Tradetracker, Bbelboon, Affilinet, Webgains, ...).
Problems usually come from the fact that programmers does not investigate to much about what remote API needs in order to connect or authenticate.
For instance PHP resends cookies from HTTP headers automatically, but when using 'node' package, it have to be explicitly set (for instance by 'soap-cookie' package)...
The simplest way I found to just send raw XML to a SOAP service using Node.js is to use the Node.js http implementation. It looks like this.
var http = require('http');
var http_options = {
hostname: 'localhost',
port: 80,
path: '/LocationOfSOAPServer/',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': xml.length
}
}
var req = http.request(http_options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.')
})
});
req.on('error', (e) => {
console.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(xml); // xml would have been set somewhere to a complete xml document in the form of a string
req.end();
You would have defined the xml variable as the raw xml in the form of a string.
But if you just want to interact with a SOAP service via Node.js and make regular SOAP calls, as opposed to sending raw xml, use one of the Node.js libraries. I like node-soap.
I managed to use soap,wsdl and Node.js
You need to install soap with npm install soap
Create a node server called server.js that will define soap service to be consumed by a remote client. This soap service computes Body Mass Index based on weight(kg) and height(m).
const soap = require('soap');
const express = require('express');
const app = express();
/**
* this is remote service defined in this file, that can be accessed by clients, who will supply args
* response is returned to the calling client
* our service calculates bmi by dividing weight in kilograms by square of height in metres
*/
const service = {
BMI_Service: {
BMI_Port: {
calculateBMI(args) {
//console.log(Date().getFullYear())
const year = new Date().getFullYear();
const n = args.weight / (args.height * args.height);
console.log(n);
return { bmi: n };
}
}
}
};
// xml data is extracted from wsdl file created
const xml = require('fs').readFileSync('./bmicalculator.wsdl', 'utf8');
//create an express server and pass it to a soap server
const server = app.listen(3030, function() {
const host = '127.0.0.1';
const port = server.address().port;
});
soap.listen(server, '/bmicalculator', service, xml);
Next, create a client.js file that will consume soap service defined by server.js. This file will provide arguments for the soap service and call the url with SOAP's service ports and endpoints.
If node-soap doesn't work for you, just use noderequest 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:
You can use wsdlrdr also. EasySoap is basically rewrite of wsdlrdr with some extra methods.
Be careful that easysoap doesn't have the getNamespace method which is available at wsdlrdr.
In my opinion, avoid querying SOAP APIs with nodejs.
Two alternatives :
If you're the owner of the SOAP API, make it handle both xml and json requests because javascript handles well json.
Implement an API gateway in php (because php handles well SOAP). The gateway will receive your input as json, then query the SOAP API in xml and transforms the xml response into json.
Scroll down untill you see Soap operation name you are interested in.
Then copy the operation soapAction = "http://xyzxyzxyz/xyz/xyz/ObtenerSaldoDeParcelaDeEmprestimo"
in the axiosCall header.
const axiosCall = require('axios')
const xml2js = require('xml2js')
let xml = `<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:tem="http://pempuri.org/"
xmlns:ser="http://schemas.example.org/2004/07/MyServices.Model">
<soapenv:Header/>
<soapenv:Body>
<tem:DocumentState>
<tem:DocumentData>
<ser:ID>0658</ser:ID>
<ser:Info>0000000001</ser:Info>
</tem:DocumentData>
</tem:DocumentState>
</soapenv:Body>
</soapenv:Envelope>
let url = 'https://somewebservice.company.com.br/WCF/Soap/calc.svc?wsdl'
axiosCall.post( url,
xml,
{
headers: {
'Content-Type': 'text/xml',
SOAPAction: 'http://xyzxyzxyz/xyz/xyz/ObtenerSaldoDeParcelaDeEmprestimo'
}
})
.then((response)=>{
// xml2js to parse the xml response from the server
// to a json object and then be able to iterate over it.
xml2js.parseString(response.data, (err, result) => {
if(err) {
throw err;
}
console.log(result)
}
})
})
.catch((error)=>{
console.log(error)
})