Sending SOAP Headers with ActionScript 2.0
September 2nd, 2007This is part of my notes while I’m trying to create a Web Service in PHP to be consumed by a SWF application written in ActionScript 2.0.
My web service is still under development and I need a way to authenticate users. I read about SOAP headers that can be sent with each SOAP call. These headers may contain authentication details (e.g: username, password).
While reading ActionScript 2 documentation I stumble upon this "technote" from Adobe/Macromedia: How to send headers in SOAP calls with the Webservices classes.
They mention an issue with SOAP headers, and provide an AS2 Class to download and fix the problem.
One of the things I liked is that you can add persistent headers to your Service object, instead of having to add your headers to every individual SOAPCall. This is great for my authentication purposes. I can add the (username/password) authentication details once in my code, and they will be sent along with every SoapCall i nthe future.
The documentation says every header you pass to AddPersistentSOAPHeader() or AddTemporarySOAPHeader() should be an ActionScript XML object. Here's an example:
-
var h1 = new XML("<sen:UserID xmlns:sen='myServiceNamespaceURI' xsi:type='sen:UserID'><username xsi:type='xsd:string'>JulianG</username><password xsi:type='xsd:string'>321321</password></sen:UserID>");
-
myService.AddPersistentSOAPHeader (h1);
-
// from now on. all the SOAP calls will be sent with the header I just added.
-
myService.anyMethod("any parameters");
This is a bit tricky, though. How do we know the exact syntax for those headers?
I'm using soapUI to test my SOAP Web Server. This tool reads the entire WSDL, which is the web service's description and automatically creates SOAP requests for each of its methods.
So I looked at the "headers" part of the SOAP call code for my testing method and it looked like this:
-
<soapenv:Header>
-
<sen:UserID xsi:type="sen:UserID">
-
<username xsi:type="xsd:string">?</username>
-
<password xsi:type="xsd:string">?</password>
-
</sen:UserID>
-
</soapenv:Header>
I figured out I didn't have to use the <soapenv:Header> node, but just the node inside it. So I tried to use that in my actionscript code. But it wouldn't work. The same headers that worked using soapUI did not work when using Flash.
So I compared the SOAP request generated by Flash against the one generated by the soapUI application.
The Flash SOAP request was missing one argument on the main node, and I couldn't find a way to add it manually.
The missing argument was a namespace prefix definition:
-
xmlns:sen="myServiceNamespaceURI"
That namespace definition is included in the WSDL, but Flash wouldn't add it to its requests for some reason. My request headers have references to that prefix sen, but if the definition is missing it doesn't work. Since I found no way to add the definition to the main node in the SOAP request, I just added it on this specific header:
-
<sen:UserID xmlns:sen='myServiceNamespaceURI' xsi:type='sen:UserID'>
-
<username xsi:type='xsd:string'>JulianG</username>
-
<password xsi:type='xsd:string'>321321</password>
-
</sen:UserID>
Now it works!
I just need to handle these SOAP headers in my PHP 5 web service. But that's for another post.
April 30th, 2008 at 11:37
Very good post guys, thanks! that namespace probelm was haunting me .... josef
October 8th, 2009 at 10:29
Bravo, me parece esto la idea excelente