Reoccurring Problems with Soap
Once in a while, rather seldom, I need to deal with a SOAP API. And whenever I do, I forget wath I am supposed to do. So, recently I was required once again to do so. Here are some steps to look at.
As I am working in node
-world I have been using the node
library node-soap
Even though I am using node-soap
, this article does not try to blame that library,
but is more due to other problems that I have or encounter using SOAP APIs.
Possible Steps to Go Along⌗
- Find out where to get the
wsdl
file - Research which functions and types you’ll need from that
wsdl
XML file - Create client using
wsdl
+ authentication parameters - Call the function required on the client.
Problems⌗
The problems I usually encounter are:
- Namespaces are wrong
- Connection problems
In order to resolve these problems, it is best for me to print out all the objects and XMLs:
- print client (JSON):
console.log(JSON.stringify(client.describe(), null, 2))
(the extra parameter inJSON.stringify
are for pretty-printing) - print the request and response XMLs. I’m afraid I could not find a better way than to edit the source in the
node_modules
:
Inclient.js
, try to find occurrences ofxml
orhttpClient.requestStream
around here to get the request XML and for the response XML around here.
Make sure to edit the corresponding.js
file instead of the.ts
ones in these samples above.
Wrong Namespaces⌗
Very often I encounter wrong namespaces. Whenever I do, I promptly blame the library.
But as usually it turns out to be a different issue. In my last case
the wsdl
used inconsistent namespaces that got me confused.
If there are indeed namespace issues, do one of:
- If you’re lucky you may be able to tweak the namespaces with the third parameter of
addSoapHeader
. Example:client.addSoapHeader(authParams, 'sampleObject', 'ns')
. - Download and fix the
wsdl
manually and then load the modified XML from the file system - Contact the API provider
Connection Problems⌗
In my last encounter with a SOAP-API, the wsdl
response had wrong URL endpoint set.
In such a case, it is possible with node-soap
to override that URL with:
client.setEndpoint(newUrl)
Testing⌗
To my surprise testing was always fairly easy. The only slightly gnarly part is to create the exact XML matches for the requests.
In nock
for example do:
soapApiUrl = 'http://the.api.provider'
nock(soapApiUrl)
.post('/soap/api/myService', soapRequestXml)
.reply(200, soapResponseXml, {'Content-Type': 'application/xml'})
Voilà. That should do it.