BackUp LevelNext

Using the CFHTTP Get Method

Use Get to retrieve text and binary files from a specified server. The examples below illustrate a few common GET operations. The Get method is a one-way transaction in which CFHTTP retrieves an object. By comparison, the Post method is a two-way transaction in which CFHTTP passes variables to a ColdFusion page or CGI program which then returns data, usually processing what was received.

Example: Retrieving to a variable

The following example uses the Get method to perform a simple file request on the default page of the Yahoo Web site (as of this writing of course) and stores the contents of the file in a variable. Using CFOUTPUT, the page is rendered from the contents of the variable and displayed in the browser.

<CFHTTP METHOD="Get"
    URL="http://www.yahoo.com/index.htm"
    RESOLVEURL="Yes">

<CFOUTPUT>
    #CFHTTP.FileContent# <BR>
</CFOUTPUT>

When FILE and PATH attributes are omitted, ColdFusion stores the contents of the file index.htm in the CFHTTP.FileContent variable. Note that, without the RESOLVEURL attribute set to "Yes," relative links in the downloaded page would be broken. When set to Yes, RESOLVEURL turns relative links into absolute links that resolve correctly.

Example: Retrieving to a file

The following example also uses the Get method to perform a simple file request, as above, but the addition of the PATH and FILE attributes results in the retrieved file being saved to the server as a variable.

<CFHTTP
    METHOD = "get"
    URL="http://www.yahoo.com/index.htm"
    PATH="c:\temp"
    FILE="yahooindex.htm">

Note that when the PATH and FILE attributes are used, the RESOLVEURL attribute is ignored, even if present. The contents of the retrieved file can be referenced in the CFHTTP.FileContents variable.

Example: Retrieving a binary file

Like the previous example, this example retrieves a file from a server and saves it to the location specified in the PATH and FILE attributes. The only difference is the MIME type of the retrieved file.

<CFHTTP
    METHOD="Get"
    URL="http://maximus/downloads/quakestuff/q2_test.zip"
    PATH="c:\quake2\install"
    FILE="quake2beta.zip">

<CFOUTPUT>
    #CFHTTP.MimeType#
</CFOUTPUT>

The CFOUTPUT block at the end displays the MIME type of the downloaded file, which in this case, is interpreted as application/zip.


BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.