Search code examples
javascriptjavanode.jsweb-servicessoap-client

Simple SOAP Client for consuming web services using javascript or java


I was working in a desktop app (with Electron framework) that consumes services from a web app, for that I was using node.js soap package to make the request and all worked well.

But suddenly I was asked to migrate the desktop app to make it a client-only web app.

My problem: I don't know how to change the node code to make it work in the browser. I have little experience with web development and I'm having a lot of doubts and problems.

What I tried:

  • Using Javascript, with XMLHttpRequest(); but I get CORS problems

  • Using Browserify to bundle the code, but I get an error from fs.readFileSync()

My questions:

  • Can I make a pure Javascript Soap request without having problems from cross-origin?? (I was told that is not possible but I want to confirm it)

  • Is there any way to use the code below (with node modules) in the browser?? (Using tools like browserify)

  • If I have to make a Java class for this request, will I have cross-origin requests blocked like with javascript?? If not how should I do it??.

If possible I would like code examples or a explanation of how the architecture should be

Domains of apps in Tomcat:

http://localhost:8080/AppWithWebServices/ws/core.wsdl --> Web service to consume http://localhost:8080/clientApp/soapRequest.js --> File with the request to the web services

Code from my desktop app with the soap request (soapRequest.js):

var soap = require('soap');
var request = require('request');
const {ipcRenderer} = require('electron');
window.ipc = ipcRenderer;

Submit = function (){
    var user = document.getElementById("username").value;
    var passw = document.getElementById("password").value;

    //Class with the wsdl url and the header and body for the request
    var auth = new authentication(user,passw);

    soap.createClient(auth.getWsdlURL(), function(err, soapClient){
       if (err){
            console.log("error en creacion cliente Soap");
       }

         //The soap request needs a security header with user and password 
         //to be able to consume the web services
         soapClient.addSoapHeader(auth.getSoapHeader());

         soapClient.ServiceToConsume(auth.soapRequestBody(), function(err, result) {
              if (!err){     
                 //Redirect to Home.html             
                 ipc.send('redirectHome', user, result);
              }
              else{            
                document.getElementById("login").innerHTML = "El nombre de usuario o contraseña es incorrecto";
              }
          });
     });

};

Solution

  • As the readme page of the soap library says, it is

    A SOAP client and server for node.js.

    Considering your error message, it appears to rely on node's file system API:

    fs.readFileSync()
    

    Unless the library supports it, you will have a hard time getting it up and running in your browser, since node's APIs are not necessarily available in this environment.

    You could look for a different SOAP library that is able to run in the browser. However, JavaScript and SOAP will never play perfectly together. In the long run, you may be better off implementing a middleware on server side that maps your SOAP service to a RESTful service serving your data in JSON format which you can easily consume from your front end without special libraries.

    Regarding the CORS issue, you can avoid this restriction when loading files from different origins, independent of the purpose of the HTTP call. MDN gives some more details on this topic.