Article

Home » Server-side Coding » ColdFusion Tutorials » Practical Uses of HTTP in ColdFusion

About the Author

David Medlock

author_davemedlock David operates MedlockWeb, a web consulting firm that focuses on building solid relationships and web applications with entrepreneurs and investors. He also maintains a web business blog at WebDevSuccess.com.

View all articles by David Medlock...

Practical Uses of HTTP in ColdFusion

By David Medlock

March 26th, 2004

Reader Rating: 8

Page: 1 2 3 Next

The Hypertext Transfer Protocol, HTTP, was first used in the "World Wide Web global information initiative" in 1990. Since then, it has become a part of our everyday lives, whether we think about it or not.

Usually, we make use of this protocol without giving it a second thought, like when we surf the Web. Sometimes, though, we have to actively use HTTP to solve problems. ColdFusion has built-in tags for HTTP support and, as Web developers, it is to our advantage to know how to use these tags efficiently. So, let's take a trip down HTTP lane and see what can be done with this technology.

How HTTP Works

If we dumb it down to the lowest level, HTTP is a way for clients to request information from a Web server, and for Web servers to provide responses to those requests. When you type http://www.sitepoint.com/ into your browser's address bar, your browser (the client) sends an HTTP request to SitePoint's server. SitePoint's server then processes your request, pulling data out of the database, creating the HTML, and returning it to your browser.

There are two kinds of requests commonly made to a server: "GET" and "POST" requests (and yes, this is why HTML forms let you set a submission method of "GET" or "POST"). The method used specifies how the data is sent. You'll notice that if you create a form with the "GET" method, the values entered in the form are passed on the URL. If you use the "POST" method, they are sent behind the scenes. That's why, for example, Internet Explorer always asks if you want to resend the data when you reload the result of a "POST" request -- it's letting you know that something was sent behind the scenes and must be resent for the request to process correctly.

We should also touch briefly on MIME types. MIME stands for Multipurpose Internet Mail Extensions. This standard was originally intended to define the types of files that are exchanged via email, but has since been extended to the World Wide Web. Knowing about MIME types will allow us to return information in various formats and it will be a key part of our demonstration code below.

There's much, much more to HTTP than I've described in these brief paragraphs. In fact, entire books have been written on the topic. But, for our purposes, we'll leave the discussion here. If you want more information, you can find an excellent HTTP reference at the W3C Website.

HTTP and ColdFusion โ€“ The Tags

First, let's cover the basic tags we'll be using in our sample application. We'll be using cfhttp, cfhttpparam, cfheader, and cfcontent.

First, let's look at CFHTTP. The most common use for CFHTTP is to retrieve data from another page or site. For example, if you wanted to put an RSS feed of my SitePoint Blog, The Fuse, on your site, you would formulate an HTTP call that would look like this:

<cfhttp url="http://www.sitepoint.com/blog.rdf?blogid=7" method="get" timeout="5">

First, you want to specify the URL of the blog with the url attribute, of course. The method we're using is "GET", and we specify that we want a five second timeout. This way, if the server is slow, it will timeout, and not hold up our application unnecessarily. The cfhttp tag returns a structure to us called cfhttp and it contains several variables that may be of use to us. First, we have the FileContent variable. This holds all the data returned to us by the URL we called. So when we display a raw text dump of CFHTTP.FileContent, we see the raw XML that is returned from the blog's RSS feed:

<cfoutput>#HTMLCodeFormat(cfhttp.FileContent)#</cfoutput>

Other variables returned by cfhttp are MimeType, Header, and ResponseHeader. ResponseHeader is a struct that contains several variables dealing with the response received from the URL. If we had attempted to access a binary file, such as a .jpg or .gif file, on a server somewhere, the MimeType would have been something like "image/jpeg" or "image/gif". You can perform a simple text dump of the cfhttp struct like this:

<cfdump var="#cfhttp#">

This works on ColdFusion 5 and up, and it will show you exactly what comes back when you use this particular tag.

You can also have ColdFusion automatically save the file to the server when it gets the information back from the URL, by passing it both the path and the file attributes like this:

<cfhttp url="http://www.sitepoint.com/blog.rdf?blogid=7"
 method="get"
 timeout="5"
 path="C:\temp"
filename="thefuse.rdf">

This will save the file directly to the server so that you can use it later. For example, if you didn't want to access SitePoint's server every time the RSS feed was displayed on your site, you could cache the file on your server using the code above, then implement some logic that would refresh the cached copy every day. It might look something like this:

<cfdirectory action="list"  
directory="C:\temp"  
name="TheFuseCache"  
filter="thefuse.rdf">

<cfif TheFuseCache.RecordCount eq 0  
or DateDiff("d", TheFuseCache.DateLastModified, Now()) gt 1>
 <!--- use the CFHTTP call as you see above and then do this:--->
 <cfset TheFuse = cfhttp.FileContent>
<cfelse>
 <cffile action="read"  
file="C:\temp\thefuse.rdf"  
variable="TheFuse">
</cfif>

Now you've got the RSS feed updated once a day and you only have to make one call per day to SitePoint's server (which will reduce their bandwidth costs and yours, ease the load on their server, and free up their resources to provide you with more excellent ColdFusion content!). Everyone wins!

Another attribute worth mentioning with regards to cfhttp is the ResolveUrl attribute. If you used cfhttp to call a site that had relative paths in its image sources, links or style sheets, you might want to set the ResolveUrl attribute to "yes". This makes sure that all the links still point to the correct path or URL when you get the file content back. Otherwise, you'll have broken links, style sheets, images, and the like if you try to reuse the raw HTML on your own site.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article