Open In App

How to perform SOAP requests with Node ?

Last Updated : 14 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

SOAP (Simple Object Access Protocol) serves as a messaging protocol in web services, facilitating the exchange of structured information between applications. Built on XML, SOAP establishes a standardized message format, enabling diverse applications to communicate seamlessly over a network.

In Node users can employ the node-soap library to develop both SOAP clients and servers. This library offers a straightforward API for interacting with SOAP web services, enabling users to make requests to external SOAP services or establish their own SOAP API for consumption by other applications.

Steps to perform SOAP requests with Node:

Step 1: Install node-soap library using NPM. Open the command prompt and run the following command:

npm install soap

Step 2: Create a new Node.js file in your project directory, and import the soap library:

const soap = require('soap');

Project Structure:

NodeProj

The updated dependencies in package.json file will look like:

"dependencies": {
"soap": "^1.0.0",
} 

Step 3: Define the URL of the WSDL file for the SOAP service you want to consume. This URL should be provided by the SOAP service provider. For example:

const url = 'http://www.dneonline.com/calculator.asmx?wsdl';

Step 4: Use the soap.createClient method to create a new SOAP client object from the WSDL file.

soap.createClient(url, function(err, client) {
if (err) {
console.error(err);
} else {
// SOAP client object is created successfully
}
});

This method takes two arguments: the URL of the WSDL file and a callback function. The callback function is executed after the client object is created, or if there’s an error in creating the client object.

Step 5: Use the client object to make SOAP requests. The client object has methods corresponding to each of the SOAP methods available in the WSDL file. For example, to call the Add method of the calculator SOAP service, use the following code:

const args = { intA: 2, intB: 3 };
client.Add(args, function(err, result) {
if (err) {
console.error(err);
} else {
// SOAP request is successful, and r
// esult contains the response data
}
});

This code sends a SOAP request to the Add method of the calculator SOAP service with two integer arguments, and the callback function receives the response data.

Step 6: Finally, run the Node.js file using the command:

node <filename>.js

This will execute the Node.js code and output the results of the SOAP request to the console.

Example 1: In this code, we’re using the URL for the SOAP service provided by dneonline.com. We’re calling the Add method of this SOAP service with two integer arguments and logging the result to the console.

Javascript




const soap = require("soap");
 
const url =
 
soap.createClient(url,
    function (err, client) {
        if (err) {
            console.error(err);
        } else {
            // Make SOAP request using client object
            const args =
            {
                intA: 2,
                intB: 3
            };
            client.Add(args,
                function (err, result) {
                    if (err) {
                        console.error(err);
                    } else {
                        console.log(result);
                    }
                });
        }
    });


Output:

Output

Example 2: In the below example, we are using a soap service to list the database by using the GetDatabasesResult method.

Javascript




const soap = require("soap");
 
const url =
 
soap.createClientAsync(url,
    {
        overridePromiseSuffix: "Promise"
    })
    .then((client) => {
        client.GetDatabasesPromise({})
            .then(
                (results) => {
                    const data = results[0]
                        .GetDatabasesResult.string;
                    console.log(data);
                });
    });


Output:

Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads