Determining the HTTP request method in ColdFusion

This post is more than 2 years old.

I was talking to someone recently about REST and ColdFusion 10 (something I'd like to demo here soon) and the question of HTTP methods came up. One of the interesting things ColdFusion 10's REST support allows for is CFC methods locked down to a particular HTTP request type. This allows you to ensure a method is run only for a GET request, or PUT, or whatever makes sense for your business logic.

But what about ColdFusion 9, and earlier? Is it possible to programatically respond to different request types? Absolutely. You've got not one, but two different ways to check how the current CFM/CFC file was requested:

<cfoutput> cgi.request_method=#cgi.request_method#<br/> </cfoutput> <cfset req = getHTTPRequestData()> <cfoutput> getHTTPRequestData().method=#req.method# </cfoutput>

In the example above, the first way we check the request method is via the CGI scope. That's simple enough. But you can also use the ColdFusion function, getHTTPRequestData. This returns the method in the result structure.

Either of these methods should work fine for you.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by Ty Whalin posted on 12/9/2013 at 7:45 PM

Okay Ray, here is one for ya. I am trying to make use of the cfheader tags. I am setting the cache controls with the cfheader tags. I set the cache-control max-age=30 for testing but when using the getHttpRequestData function to retrieve information about the requested page, it shows the cache-control max-age=0. Why is the cache-control max-age still being set to 0?

Comment 2 by Raymond Camden posted on 12/9/2013 at 9:32 PM

Not sure on that one. Do you have a test file I can try running?

Comment 3 by Ty Whalin posted on 12/10/2013 at 12:43 AM

This is what I have been working with for now. Think it is actually working correctly now. Trying to increase page speed with browser caching. I have been using the cfcache tag as well.

<cfheader name="cache-control" value="no-cache">
<cfheader name="cache-control" value="public">
<cfheader name="cache-control" value="must-revalidate">
<cfheader name="cache-control" value="max-age=3600">
<cfset dtExpires = (now() + 1) />
<cfset strExpires = getHttpTimeString(dtExpires) />
<cfheader name="expires" value="#strExpires#">
<cfheader name="pragma" value="no-cache">
<cfset strGMT=(DateFormat(now()+1, "ddd, dd mmm yyyy") & " " & TimeFormat(now(), "HH:mm:ss") & "GMT") />
<cfset lastGMT=(DateFormat(now()-1, "ddd, dd mmm yyyy") & " " & TimeFormat(now(), "HH:mm:ss") & "GMT") />
<cfheader name="last-modified" value="#lastGMT#">
<cfheader name="cache-control" value="max-age=3600">
<cfheader name="date" value="#getHttpTimeString()#">
<cfset etag = application.VersionKey />
<cfif (cgi.http_if_none_match eq etag)>
<cfheader statuscode="304" statustext="Not Modified" />
<cfexit />
</cfif>
<!--- <cfheader name="etag" value="#createUUID()#"> --->
<cfheader name="etag" value="#etag#" />

Comment 4 by Raymond Camden posted on 12/10/2013 at 1:42 AM

Ah ok - well - glad you got it working.

Comment 5 by Ty Whalin posted on 12/10/2013 at 4:05 AM

Now I am playing with cfcache functions mixed with the Application.cfc. Little more advanced, but making some progress. The problem is when I run the speed suggestions test in GA, it shows (expiration not specified) for browser caching. Still trying to leverage browser caching and set a expiration on the page/files. All suggestions appreciated. I have managed to make the pages cache with:
<cfcache action="cache" protocol="http://" timespan="#CreateTimeSpan(0,0,30,0)#" idletime="#CreateTimeSpan(0,0,5,0)#" stripWhiteSpace="true" throwOnError="true" usequerystring="true">- - content - -</cfcache> No dynamic content only. I do know the new cache tag can also cache dynamic content as well.

I have even made use of a custom cache and output the content on the page with it. But I found using this method was somewhat confusing since the cache wrapped, say an image and then output the same image on the page.

Comment 6 by Ty Whalin posted on 12/10/2013 at 8:37 AM

Okay, how about this one. I am using the cfcache on the page. The cfache works, but the CFIDE cfform.js and masks.js are duplicating in the header and the <cfhtmlhead text="<link href='http://#CGI.HTTP_HOST#/favicon.ico' rel='Browser Icon' type='image/x-icon'/><title>page title</title>"/> is duplicating; along with the generated _CF_checkCFForm_1 = function(_CF_this) scripting as well. Know of a cfcache bug?

Comment 7 by Ty Whalin posted on 12/10/2013 at 9:55 AM

Next, I removed all forms of cfcache and added this cfache to the page right above the body tag. <cfcache action="clientcache" timespan="#createTimeSpan(0,0,30,0)#" protocol="http://" idletime="#createTimeSpan(0,0,5,0)#" stripWhiteSpace="true" throwOnError="true" useCache="true">

If I close the tag anywhere on the page it produces the duplicate issue's again. My test page to flush and check for caching show the page as caching. Is there anyway to show whether the page is using the server side cache or the clientcache? I currently have it set to client side caching since the page speed tester is asking for browser caching, which is on the client side. What do you think now?

Comment 8 by Raymond Camden posted on 12/10/2013 at 6:12 PM

When you do <cfcache></cfcache> the behavior is different than just one <cfcache>. <cfcache></cfcache> will cache just the stuff on the inside - but the form stuff you use may still be trying to inject into the header anyway. You may have found a bug here though. Best I can say is file it.

And *stop* using cfform!

Comment 9 by Ty Whalin posted on 12/12/2013 at 6:24 AM

Right on Ray. Since the last comment, I have converted a few pages to HTML5, removed the useCache=true function, added a cache.appache manifest file for all HTML5 pages, ran a test to show the browser is now caching the pages, left the cfcache tag without any closing tag for it on the page above the body and removed the cfheader tag's. I also shifted the cfheader tag's to meta cache-control.

After removing the useCache=true and still not putting a close tag or trailing slash on the cfcache tag, I have managed to avoid the duplication issue with the cfform.js, masks.js and any cfhtml title tag's. This has produced a server cache for sure and now with the manifest file a browser cache.

In the GA account it shows the server response time as a go but the files needing to be cached by the browser as still not having an expiration date. So I have now contacted the hosting company to ask questions. They have stated that this is a recent issue several clients have submitted tickets for and plan to send me an update soon.

The next step is to possibly add the files needing cached - to the .htaccess file. Will keep you posted. By the way I just got the test release from Adobe for the new Splendor and Thunder. How about you?

Comment 10 by Raymond Camden posted on 12/13/2013 at 6:04 PM

"By the way I just got the test release from Adobe for the new Splendor and Thunder. How about you?"

Ahem - you aren't supposed to talk about that publicly. ;)

Comment 11 by Ty Whalin posted on 12/14/2013 at 10:35 PM

I read the agreement, but do not recall it saying anything about not being able to say I am involved in the testing. But you probably are right. To be on the safe side then, just delete the comment. Thanks

Comment 12 by Raymond Camden posted on 12/15/2013 at 12:29 AM

Nah, don't stress over it. ;)

Comment 13 by Ty Whalin posted on 12/15/2013 at 8:15 AM

Well, for the final outcome; I ended up getting a total output performance score of 98 out of 100. Could not get it to 100% total. Close enough. Here is the page I just finished if you care to take a quick look at it. Or actually just click the link on my name. I thought about changing the max-age time. What would you think is suitable. I found 3600 to be the average for the most part.

Comment 14 by Raymond Camden posted on 12/16/2013 at 7:19 AM

Seemed to load pretty darn quick to me. I think at this point we are a bit off topic now from the main blog topic so if you have more on this, ping me directly.

Comment 15 by Ty Whalin posted on 12/22/2013 at 2:16 AM

A tad off topic, but it all revolved around caching problems. I ended up utilizing a script for the Safari console to determine if the pages were caching or not. Plus I added a manifest file for HTML5 and deferred certain scripts to help along. Although I lost my event tracking when trying to defer the analytics code so I lost some page performance when not defering the analytics code. But still loads pretty quick. Appreciate all the help - Thanks.

Comment 16 by Link Worx Seo posted on 7/27/2016 at 10:14 PM

Oddly enough, I made my way back to your post. Have come a long ways since this set of post before. I All caching problems solved and have converted to almost completely cfscript and HTML5, Upgraded my CMS system, played with Mura, and am currently creating another website for my business.