Using SOAP Request Headers in CFMX 6.1

By David Fekke
January 17th, 2011

One of the developers I work with recently had a problem trying to make Web Service calls to a SOAP based Web method that requires Request Headers. I ran into the same problem about a year ago using CFMX 6.1. Request and response headers for web services are now built into CFMX 7. It is possible to get CFMX 6.1 to send request headers. Here is how you can do it with CFMX 6.1. The first thing you will have to do is make sure CFMX is patched to at least the 6.1 updater (6,1,0,83762) or the hf53566_61.jar hot fix. This patch will cause problems with trying to use cfdump to output the cfcatch variable, but there is also a patch for that as well.

You will need to use the createObject function to create a web service object instead of using cfinvoke to be able to use the request header.

"webservice","[](") />
Request headers are usually in the form of a XML object. You can use the CFMX's built features to create the xml object.

<cfset doc.Authentication = XmlElemNew(doc, " ", "Authentication") />

<cfset doc.Authentication.username = XmlElemNew(doc, "username") />

<cfset doc.Authentication.username.XmlText = "myUsername" />

<cfset doc.Authentication.password = XmlElemNew(doc, "password") />

<cfset doc.Authentication.password.XmlText = "myPassword" />

The resulting XML looks like this when serialized;



The object that is passed to to the request header method requires the use of some java, but it is very simple. The first thing that needs to happen is to turn the ColdFusion XML Object into something that Java will understand. There is a function built into every ColdFusion XML object that returns a Java based XML object.
Once you have the java based XML object, you will need to create a SOAPHeaderElement java object that will be passed into the web service object.
"java","org.apache.axis.message.SOAPHeaderElement") />

<cfset headerElement.init(jXMLdoc) />

<cfset WSObj.setHeader(headerElement) />

Now that the request header has been set in the web service object, it is safe to call the methods that require that request header.
I plan on creating a web service on my web site that use request and responce headers. Thanks to Tom Jordahl and Damon Cooper who showed me how to get this to work in ColdFusion 6.1.

ColdFusion MX 7 handles this now quite well by using addSOAPRequestHeader() function. There is no longer a need to call the underlying java to make these calls.

← Previous Page  Next Page →