Sapan Diwakar

Software developer

Follow me on Twitter Check out my code on GitHub View some of my designs on Dribbble Take a look at my Linked In profile

Sending SOAP requests with Appcelerator Titanium

Simple Object Access Protocol (SOAP) is a standard protocol specification for message exchange based on XML. Communication between the web service and client happens using XML messages. For one of the mobile clients I was working on, I had to write a client for communicating with a SOAP service. Sending a SOAP request is quite easy using the [cci lang="javascript"]Titanium.Network.HTTPClient[/cci] object.

[cc lang="javascript" lines="-1"] /** * Get the beginning of request envelope for SOAP request */ function getRequestEnvelopeBegin() {
return ''; }

/** * Get the end of request envelope for SOAP request */ function getRequestEnvelopeEnd() {
return ''; }

/* ** * Get the a new XHR object */ function getXHR() {
var xhr; //Grab an XMLHTTPRequest Object xhr = Titanium.Network.createHTTPClient(); return xhr; }

/** * Sends a SOAP request and calls the callback * @param {Function} callback */ function sendSOAPRequest(callback) {
var xhr = getXHR(); // Set callback function xhr.onload = function() {'Received response for soap request.');, this.responseXML); // call the callback with response xml };

// Create body for request var soapRequest = getRequestEnvelopeBegin() + '' + getRequestEnvelopeEnd();

// Send soap request'POST', url); xhr.setRequestHeader('Content-Type', 'text/xml'); xhr.setRequestHeader('SOAPAction', SOAPREQUESTACTION); xhr.send(soapRequest); } [/cc]

One important piece of information in the SOAP request other than the body is the header for the request. Most of the servers wouldn't handle your requests until the headers for the requests are what they expect.