Adobe ColdFusion 8

Using request and response headers

ColdFusion includes a set of functions that enable your web service to get and set request and response headers. You use these functions to retrieve the response headers from a web service request and to create SOAP headers in a request that has the mustUnderstand attribute set to be True.

You typically use different functions in web services clients and in the web service CFC, itself:

In the client:

  • , called before the request to set a SOAP header.
  • , called after the request to retrieve a SOAP header.

In the web service CFC:

  • , called to determine whether the CFC method is being called as a web service.
  • , called to retrieve a SOAP header set by the client.
  • , called to set a SOAP header that is returned to the client.\

Note: When used in a CFC, you can only use these functions in CFC methods if they are being used as web services. Use the IsSOAPRequest function to determine whether the CFC method is being called as a web service.

The following example CFM page uses the AddSOAPRequestHeader, getSOAPRequest, and GetSOAPResponse functions:

<cfsavecontent variable="my_xml">
<Request xmlns="http://www.oasis-open.org/asap/0.9/asap.xsd">
<SenderKey>ss</SenderKey>
<ReceiverKey>zz</ReceiverKey>
<ResponseRequired>Yes</ResponseRequired>
<RequestID>id</RequestID>
</Request>
</cfsavecontent>
<cfset xml_obj = xmlparse(my_xml)>

<cfscript>
ws = CreateObject("webservice", "http://localhost:8500/soapexamples/HeaderFuncs.cfc?WSDL");
addSOAPRequestHeader(ws, "http://www.cfdevguide.com/", "testrequestheader", "#xml_obj#");
</cfscript>

<cfscript>
ret=ws.showSOAPHeaders();
inxml = getSOAPRequest(ws);
outxml = getSOAPResponse(ws);
</cfscript>

<cfoutput>
<h2>Return Value</h2>
<!--- This is XML, so use HTMLCodeFormat. --->
The return value was #ret#
<h2>Complete Request XML</h2>
#htmlcodeformat(inxml)#
<h2>Complete Response XML</h2>
#htmlcodeformat(outxml)#
</cfoutput>

The following example CFC uses the IsSOAPRequest and AddSOAPResponseHeader functions:

<cfcomponent>
<cffunction 
 name = "showSOAPHeaders" 
 returnType = "string" 
 output = "no"
 access = "remote"
     hint="After calling this function, use GetSOAPRequest and GetSOAPResponse to view headers">
    <cfset var xml_obj = "">
    <cfset var ret = "">

<cfif IsSOAPRequest()>
<!--- Define a response header --->
<cfsavecontent variable="response_xml">
 <ThisResponseHeader xmlns="http://www.cfdevguide.com">
 <CreatedDateTime><cfoutput>#now()#</cfoutput></CreatedDateTime>
 <ExpiresInterval>6000</ExpiresInterval>
 </ThisResponseHeader>
</cfsavecontent>
<cfset xml_obj = xmlparse(response_xml)>
<!--- Add the response header --->
<cfscript>
addSOAPResponseHeader("http://www.cfdevguide.com/", "testresponseheader", "#xml_obj#");
ret = "Invoked as a web service. Use GetSOAPRequest and GetSOAPResponse to view headers.";
    </cfscript>
<cfelse>
<cfset ret = "Not invoked as a web service">
</cfif>
<cfreturn ret>
</cffunction>
</cfcomponent>