I had a project where I needed to setup a server that communicated over the SOAP protocol. I use Node.js for my backend server, so I began looking for libraries to use to work with.

I settled on strong-soap, but began to have issues with doing simple changes to the XML like editing the prefix for the namespace. I opened an issue for this, and there didn’t seem to be an easy way to remedy it.

It turned out the easiest and most robust solution was to explicitly write out my own XML and use request-promise-native to do a POST request with that custom crafted XML as the body to the server I was trying to communicate with.

Using ES6 I could use string interpolation to insert the values into the XML from the express server I set up to wrap around the SOAP request.

So essentially I could make POST requests to my server using JSON, and get a JSON response. And voilà, a JSON wrapped interface for a SOAP protocol.

A somewhat cut-down version of my actual code (that includes setting up https as it seems ever more important) is this…

The main advantages to this approach are:

  • No extra libraries needed
  • I can be 100% confident in what XML I am actually sending to the server. I had the SOAP 3rd party asking for the exact XML fragment in order to help troubleshoot issues with me. Using strong-soap I wasn’t sure as it was generated.

Any questions or thoughts let me know! Thanks.