Skip to main content

Javascript SOAP Client

Sometimes it is nice to get data using a SOAP call. However with using Javascript there are some disadvantages and hurdles that you may need to over come. As with any fun little project you will need some materials before we get started.

  1. Favorite Text Editor
  2. Browser
  3. Endpoint for the SOAP call - should be a url
  4. SOAP request string

If you have all these things ready to go, the next thing we need to make sure of is that the proper headers are set on the server side. If this is not your server that your are trying to hit then you will need to ask the administrator of the server if they can fix up the proper access.

The Headers we need are

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: SOAPAction, Content-Type

If you happen to have a PHP SOAP server you can add these lines to the top of your code. Please remember that the header functions will need to load before you start printing other text to the page.

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Allow-Headers: SOAPAction, Content-Type");

On the server side of things you can do a SOAP request with out all the Access Control headers. These are used as security precautions on the Browser side to prevent people from possible hacks. If you use this method to get data make sure that you properly analyze the security risks.

If all your headers are set properly then you are ready to make a SOAP call using Javascript. Copy the following code to your html file and view the alert in the browser with the return code. You can adjust the xml and url to fit your circumstances.

var xml = "YOUR-SOAP-REQUEST";
var url = "";
var xmlhttp= new XMLHttpRequest();"POST", url, true);
xmlhttp.onreadystatechange = function () {
 if (xmlhttp.readyState == 4) {
xmlhttp.setRequestHeader("SOAPAction", "");
xmlhttp.setRequestHeader("Content-Type", "text/xml");

Bonus Points : If you happen to use HTTP Auth on your web server, you can make the url look like the following and replace the username and password with your credentials


Popular posts from this blog

JSON Schema testing with Postman

Postman is becoming quite the popular tool for accessing and testing REST api services. One of the cool features is that you can write some Javascript tests on your responses. Built into Postman is also the Tiny Validator v4 and Cheerio. Cheerio is a small core JQuery like implementation. The problem I have is that there is no way to import a schema file from a remote place or a file. This will be about how I get around that issue.Pre-request Script var uri=''; $.get(uri + '/someFolder/schema.json', function(schema) { postman.setEnvironmentVariable('schema', JSON.stringify(schema)); }); This Pre-request script is what saves the day. There are a couple of timing issues that are present due to async processing. If you were to put this in your test script your chances of it not completing in time are very high and you would get inconsistent results. If you are familiar with JQuery the $.get should look familiar as a ajax call that d…

Mock HTTP calls with Angular and Protractor

One of the benefits using AngularJS is also using some test tools like Protractor. Protractor is a tool that was made for Angular testing. Many times when testing you will need to mock some data so that you can see if your application responds in the correct manner. Let us assume that we have the following HTML file running our application.
<!doctype html> <html ng-app="demo"> <head> <title>Hello</title> <script src=""></script> <script src=""></script> <script src="hello.js"></script> </head> <body> <div ng-controller="Hello"> <p>Hello <span id="gname">{{}}</span></p> </div> …

Remote API useful tricks for Docker on Mac OS X

Install SocatSocat allow you to forward ports to a variety of things. For Docker forwarding a port to the docker.sock file makes a quick way to get access to the Docker remote api. Docker also allow a way to open up that TCP port however on Mac it is temperamental and hard to usebrew install socat socat -d TCP-LISTEN:2375,reuseaddr,fork UNIX:/var/run/docker.sock & Create an IP aliasRunning Docker containers locally is a fairly common thing. Interconnectivity between containers works naturally. However, sometimes you may also want a container to connect back to the Docker Remote API. If you are constantly on the go changing networks you do not want to have to change that IP address every time. A great use case for this is running Jenkins and using docker slavessudo ifconfig lo0 alias Remote API There are also some interesting endpoints you can use for informationcurl http://localhost:2375 curl http://localhost:2375/containers/json curl http://localhost:2375/image…