Complex CF Webservices problem - solved

Couple of days ago I ran into ColdFusion Webservices problem using cfinvoke and complex arguments. The Java Webservice I was talking to expected 3 arguments - 2 strings and 1 array of structures. So far, I only had to pass String arguments to webservices, which worked fine. For some reason, and correct me if I'm wrong, ColdFusion doesn't seem to translate complex variables into correct soap-equivalent format when sending the soap packet out. I got the following error - "Web service operation 'myMethod()' with parameters {args} could not be found". I was definitely calling the right method, but CF Array I was sending was not recognised by the Webservice.

I was pretty determined to get this working and decided to build the SOAP envelope manually and send it to the webservice using a CFHTTP POST. This actually worked very nicely and this is how I did it:

Build the array of structures in SOAP format like so:

<cfsavecontent variable="soap">
<soap:myArray>
<soap:item>
<soap:firstName>John</soap:firstName>
<soap:lastName>Smith</soap:lastName>
<soap:age>27</soap:age>
</soap:item>
<soap:item>
<soap:firstName>Chris</soap:firstName>
<soap:lastName>Jones</soap:lastName>
<soap:age>30</soap:age>
</soap:item>
</soap:myArray>
</cfsavecontent>

This is only a portion of the entire SOAP envelope. See SOAP Specifications for complete soap format.

After you've got your SOAP built up, all you need to do is post it:

<cfhttp url="http://www.mydomain.com/webservice.wsdl" method="post">

<cfhttpparam type="header" name="SOAPACTION" value="wsMethod" />
<cfhttpparam type="header" name="Content-Type" value="application/soap+xml; charset=utf-8" />
<cfhttpparam type="header" name="Cache-Control" value="Cache-Control: max-age=0, must-revalidate, proxy-revalidate, no-cache" />
<cfhttpparam type="body" value="#soap#" />
</cfhttp>

As you can see, you must specify "SOAPACTION" in the header, where SOAPACTION is the Webservice method you are calling. I'm pretty sure that the other 2 parameters are optional, but I thought I'd throw them in just in case.

I haven't tested the efficiency of this approach, but I think this might actually be a quicker way of talking to webservices. The only time consuming part would be creating the SOAP envelope manually and possibly parsing the SOAP response, but if the transaction speed is important to you it might be worth considering this option - that's if it is quicker :). I will try to post some timing results soon.

Related Blog Entries

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.