Getting Header Values From A ColdFusion Request

Posted December 9, 2008 at 9:35 AM by Ben Nadel

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:

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

 
 
 
 
 
 
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!



Reader Comments

Dec 9, 2008 at 10:25 PM // reply »
15 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 »
11,238 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 »
11,238 Comments

@Cesar,

Grab it out of the headers:

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


Feb 10, 2010 at 5:13 AM // reply »
56 Comments

Hi Ben,

there's a typo in the third line of your example code (cgi.serer_name - should be server I assume).

On a different note, how can I per request set a HTTP-header for U-AX-Compatability (IE8 weirdness)? Instead of this <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> I want to do this
<?php header('X-UA-Compatible: IE=EmulateIE7'); ?>.

Can I just do this: <cfheader charset="UTF-8" name="X-UA-Compatible" value="IE=EmulateIE7"> and place this in my Application.cfm/cfc?


Feb 10, 2010 at 8:16 AM // reply »
11,238 Comments

@Sebastiaan,

Huh, good catch on the typo. I have no idea how that demo would have run (I pretty much copy/paste my actual code). I must have accidentally deleted the character. You are right - should be "server".

As far as the IE compatibility, I am not sure that that is something you can try to override with the site headers. Maybe it is - I know extremely little about it (I haven't even downloaded IE8 yet.. shame shame).

I'll have to do some research before I can answer that.


Feb 10, 2010 at 3:56 PM // reply »
56 Comments

Hi Ben,

I've done the research for you ;-) C here: http://onlinebase.nl/blog/index.cfm?fuseAction=dspEntry&entry=127&category=9

My question actually boils down to the cfheader issue!

Thanx up front.


Jan 25, 2012 at 9:57 AM // reply »
31 Comments

@Don,

The binary content of the file is easy without using CFFile action="upload", check the value of the form field you used for the upload. It'll contain the path to where CF has stored the upload until you're ready to use it.

And if you're after the original filename that it was uploaded with. Well that's hidden away in the form scope...

http://misterdai.wordpress.com/2010/06/23/form-scope-hidden-upload-details/


Apr 13, 2012 at 9:49 AM // reply »
1 Comments

Shouldn't it be

  • CGI.server_name

not

  • CGI.serer_name

?


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools