Node.js: how to consume SOAP XML web service

I found the issue its been said about headers, but I don't know where should I get it where should I put it to compile, could you explain, please?

Commented Dec 28, 2011 at 17:54

Probably you can get 'em via package management tools for your OS. On Ubuntu for example sudo apt-get install libexpat1-dev

Commented Dec 28, 2011 at 18:28

@RobertBroden, thanks for the update. Please next time go ahead and edit the answer (or suggest an edit)!

Commented Nov 2, 2016 at 13:05

I think that an alternative would be to:

Yes, this is a rather dirty and low level approach but it should work without problems

answered Apr 6, 2014 at 17:24 tmanolatos tmanolatos 932 10 10 silver badges 16 16 bronze badges

Sadly, this is the most reliable method for interacting with SOAP with Node.js. I've yet to find a single soap library that properly makes soap requests on the handful of API's I have to use.

Commented Dec 11, 2014 at 20:22 100% dirty, but brought me to results))) Commented Sep 1, 2015 at 20:08 what do you all mean with to form input xml` exactly ? Commented Jan 24, 2016 at 15:29 yeah, can confirm still, non of above mentioned libs works perfect. Commented Nov 16, 2016 at 16:19 I think "Form input xml" means just giving a Content-Type of "text/xml" Commented Aug 4, 2017 at 16:13

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:

  1. downloaded SoapUI on my Linux machine.
  2. copied the WSDL xml to a local file
    curl http://192.168.0.28:10005/MainService/WindowsService?wsdl > wsdl_file.xml
  3. In SoapUI I went to File > New Soap project and uploaded my wsdl_file.xml .
  4. 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

I used the above to build the following node request :

var request = require('request'); let xml = `  ` 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(); parser.parseString(body, (err, result) => < console.log('JSON result', result); >); >; console.log('E', response.statusCode, response.statusMessage); >; request(options, callback); 
answered Aug 29, 2017 at 2:26 4,743 5 5 gold badges 49 49 silver badges 76 76 bronze badges

thanks @jtlindsey. But i am getting 405 method not allowed as response.statusCode, response.statusMessage. By any chance do you know how to fix this?

Commented Oct 29, 2018 at 18:42

There was issue with my URL. I was using the original URL instead of the endpoint generated by SOAPUI. Thanks for the above code.

Commented Oct 29, 2018 at 18:52

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.

const express = require('express'); const soap = require('soap'); const url = 'http://localhost:3030/bmicalculator?wsdl'; const args = < weight: 65.7, height: 1.63 >; soap.createClient(url, function(err, client) < if (err) console.error(err); else < client.calculateBMI(args, function(err, response) < if (err) console.error(err); else < console.log(response); res.send(response); >>); > >); 

Your wsdl file is an xml based protocol for data exchange that defines how to access a remote web service. Call your wsdl file bmicalculator.wsdl

                      WSDL File for HelloService