Last week, Ray Camden commented on my post about sending multiple data parts in a CFHttp post. In his comment, he demonstrated how he posted custom header data to a Google API. While it was clear that I misunderstood the original question, seeing Ray's comment got me thinking; in ColdFusion I have only ever set Header values for a response or a CFHttp post - I have never gotten them from the current ColdFusion request.
The hard thing about testing this is that I sort of need two different pages - one that sets custom Header values and then one that reads them. I suppose I could have just used one page and read the Header values that came from the browser; but, I wanted to be able to see custom values that I set (which is more satisfying). First, I created a page that posted a CFHttp FORM and then CFDump'd out the response:
<!--- Build the calling URL. ---> <cfset strURL = ( "http://" & CGI.serer_name & GetDirectoryFromPath( CGI.script_name ) & "response.cfm" ) /> <!--- Make a post. ---> <cfhttp method="post" url="#strURL#" username="epicenter" password="epicenter" result="objPost"> <!--- Send header value. ---> <cfhttpparam type="header" name="Sarah" value="Stubby" /> <!--- Send form data. ---> <cfhttpparam type="formfield" name="test" value="value" /> <cfhttpparam type="file" name="data_file" file="#ExpandPath( './data.txt' )#" /> </cfhttp> <!--- Output response content. ---> <cfoutput> #objPost.FileContent# </cfoutput>
As you can see, we are simply posting some form data and a custom Header value: "Sarah=Stubby".
Once I had this page in place, I started to experiment with response.cfm to see how I could access the Header data. As it turns out, my first test was the only one I needed:
<!--- Dump out the request data. ---> <cfdump var="#GetHttpRequestData()#" label="GetHttpResponseData() Values" />
When we run the above code, we get the following output:
As you can see from the above CFDump, the posted Header values are readily available in the ColdFusion struct:
Wow, that was easy!
Want to use code from this post? Check out the license.