Consuming SOAP XML Web Services in Node.js

Nov 24, 2025 · Programming · 9 views · 7.8

Keywords: Node.js | SOAP | XML | Web Service | node-soap

Abstract: This technical article provides an in-depth guide on how to consume SOAP XML web services in Node.js. It covers the use of popular libraries such as node-soap and strong-soap, along with alternative methods using the request module and XML parsing. Step-by-step code examples are included to illustrate key concepts.

SOAP (Simple Object Access Protocol) is a messaging protocol used in web services, often relying on HTTP for transport. In Node.js, consuming SOAP services can be streamlined using specialized libraries or by manually handling XML requests and responses, depending on the use case.

Understanding SOAP and WSDL

SOAP messages are structured in XML and include elements like Envelope, Header, and Body, ensuring standardized data exchange. WSDL (Web Services Description Language) defines the service's operations and parameters, similar to REST API documentation but in XML format. Grasping these fundamentals is crucial for implementing SOAP clients in Node.js effectively.

Recommended Libraries for SOAP

Based on community best practices, it is efficient to use libraries such as node-soap, strong-soap (a rewrite of node-soap), or easysoap. These libraries abstract the complexity of SOAP messaging, providing user-friendly APIs that reduce development effort. For instance, node-soap can automatically generate client code from WSDL files, simplifying service invocation.

Example Using the node-soap Library

First, install node-soap via npm: npm install soap. Here is a simplified example of consuming a SOAP service, demonstrating how to create a client and call a service operation:

const soap = require('soap');
const url = 'http://example.com/service?wsdl';
const args = { parameter1: 'value1', parameter2: 'value2' };

soap.createClient(url, function(err, client) {
  if (err) {
    console.error('Error creating client:', err);
  } else {
    client.someOperation(args, function(err, result) {
      if (err) {
        console.error('Error calling operation:', err);
      } else {
        console.log('Result:', result);
      }
    });
  }
});

This code initializes a SOAP client and invokes an operation defined in the WSDL, handling XML serialization and deserialization automatically.

Alternative Approach with Request Module and XML Parsing

If libraries are not suitable, you can use the request module to send raw SOAP requests and parse responses with xml2js. This approach offers more control but requires manual XML handling. Example:

const request = require('request');
const xml2js = require('xml2js');

const xmlBody = `<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <someOperation>
         <parameter>value</parameter>
      </someOperation>
   </soapenv:Body>
</soapenv:Envelope>`;

const options = {
  url: 'http://example.com/service',
  method: 'POST',
  body: xmlBody,
  headers: {
    'Content-Type': 'text/xml;charset=utf-8',
    'SOAPAction': 'someAction'
  }
};

request(options, (error, response, body) => {
  if (!error && response.statusCode === 200) {
    const parser = new xml2js.Parser({ explicitArray: false });
    parser.parseString(body, (err, result) => {
      if (err) console.error('Parse error:', err);
      else console.log('Parsed result:', result);
    });
  } else {
    console.error('Request error:', error);
  }
});

This method involves manually constructing the XML request body and processing the response, suitable for custom requirements or when libraries are not supported.

Best Practices and Conclusion

When consuming SOAP services in Node.js, prefer established libraries like node-soap for simplicity and reliability. For specific needs, the request-based approach can be effective, and tools like SoapUI can help validate requests and responses. Overall, selecting the right method enhances integration capabilities and handles SOAP protocol complexities efficiently.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.