Here is an interesting question - how do you determine if something has been added to the response header? I needed to know this because of an bug in ColdFire where cflocation stopped working when ColdFire was used. Turns out that when you use cflocation, the debug template is still executed. I'm not sure why the template still runs - but when my code set information in the header, it apparently broke the data that cflocation had also set in the header. So obviously I needed to see if a cflocation header existed - and if so - suppress ColdFire. Easy, right?
Turns out it isn't that simple. ColdFusion provides a way to get request headers (getHTTPRequestData), but there is no built in way to get response headers. Luckily this is something built into the plumbing down in Java, and ColdFusion provides us a way to get there with getPageContext(). Consider the following code block:
<cfheader name="foo" value="1">
<cfset response = getPageContext().getResponse()>
<cfif response.containsHeader("foo")>
yes
<cfelse>
no
</cfif>
<p>
<cfif response.containsHeader("foo2")>
yes
<cfelse>
no
</cfif>
I set a header named foo. This is the header I'll be testing for. I then grab the page context object and from that grab the response object. How did I know to do this? I just looked around the Java API docs a bit. That is where I discovered that the response object has a "containsHeader" method that does exactly what it sounds like - returns true or false if the response contains a particular header.
If you find you need this functionality, try this simple UDF I wrote. (Which will be up on CFLib later today.)
<cfscript>
function responseHeaderExists(header) {
return getPageContext().getResponse().containsHeader(header);
}
</cfscript>
Archived Comments
Excellent stuff, I unfortunately experienced that bug and it had me going nuts for a while until I remembered it could be ColdFire. I'm looking forward to the next release!
That is some really cool stuff.
Tumbled onto this thread whilst trying to figure out how to read the Response Headers.
Is this still valid for CF10 and/or CF11? Or is there a different/better technique?
I have set a Response Header at the Site Level in IIS7.5+. I can see it in Firebug, but your code doesn't seem to read that value.
Thanks...Rick...
Still valid? I suppose so. There isn't a new way to do it so I would still use it.
As for it not showing up, it may be, and I'm guessing her, that IIS sets it AFTER ColdFusion has returned HTML back to it.
Okay man, got one for ya. I have been trying to set cache with CF11 cache tags and skipping the web.config file. I have determined from using various tools like Fiddler and a few sites for testing cache etc... that the etag can be created when the web.config file enables cache, max-age etc... The when I test using an online tool to determine if caching is working it does show cache-control works and the files being cached along with a prompt of etag is working.
When trying to fetch the ETag header this way: etags = getHttpRequestData().headers['ETag']; it does not work. Why if the ETag is being generated by the web.config file enabling cache is it that the ETag can not be obtain from the CF code above?
I know that when trying to create cache with cfcache tags it does work, scripting style or regular cache tags, but when I remove the cache options from the web.config file and then test it for caching, it does not show any files being cached or an ETag in the online tester. Any clues for me old man?
If you mean me - this old man has no idea - outside of using Node. ;)
Yes, I meant you. Hope you are doing well. Okay, not the response I expected but what the heck thought I would say hey. Thanks though. Are you still doing CF or just node.js?
I only do CF consulting as a side job here and there.