Getting Header Values From A ColdFusion Request

Posted December 9, 2008 at 9:35 AM

Tags: ColdFusion

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- Dump out the request data. --->
  • <cfdump
  • var="#GetHttpRequestData()#"
  • label="GetHttpResponseData() Values"
  • />

When we run the above code, we get the following output:

 
 
 
 
 
 
GetHttpRequestData() Can Be Used To Read Headers Values In A ColdFusion Request. 
 
 
 

As you can see from the above CFDump, the posted Header values are readily available in the ColdFusion struct:

GetHttpRequestData().Headers

Wow, that was easy!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Dec 9, 2008 at 10:25 PM // reply »
8 Comments

Nice

Now, show us how to use the binary content to upload a file without having to use cffile=upload? :)


Dec 9, 2008 at 11:43 PM // reply »
6,516 Comments

@Don,

I love CFFile - trying to upload without it would be cruel and unusual (like it is in ASP). The "content" is not just the file. The binary data should contain the file and the form field that we are submitting. However, as you can see form the CFDump, the binary data is empty. To be honest, I am not sure where the binary data goes. I don't know if this is part of ColdFusion 8's new file handling or if the form post data is stored elsewhere. I will look into this.


Oct 28, 2009 at 3:05 PM // reply »
1 Comments

Hi Ben-

How can I get the Accept-Language HTTP Request item so that I can assign to a variable?

Thanks
Cesar


Oct 28, 2009 at 3:17 PM // reply »
6,516 Comments

@Cesar,

Grab it out of the headers:

getHttpRequestData().headers[ "Accept-Language" ]


Post Comment  |  Ask Ben

Recent Blog Comments
aha
Nov 22, 2009 at 7:42 AM
Using A Name Suffix In ColdFusion's CFMail Tag
Why not? ... read »
Nov 22, 2009 at 7:37 AM
Using A Name Suffix In ColdFusion's CFMail Tag
asd ... read »
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »