Using the CF.http function



The CF.http function returns an object that contains properties, also known as attributes. You reference these attributes to access the contents of the file returned, header information, HTTP status codes, and so on. The following table shows the available properties:

Property

Description

Text

A Boolean value indicating whether the specified URL location contains text data.

Charset

The charset used by the document specified in the URL.

HTTP servers normally provide this information, or the charset is specified in the charset parameter of the Content-Type header field of the HTTP protocol. For example, the following HTTP header announces that the character encoding is EUC-JP:

Content-Type: text/html; charset=EUC-JP

Header

Raw response header. The following is an example header:

HTTP/1.1 200 OK

Date: Mon, 04 Mar 2002 17:27:44 GMT

Server: Apache/1.3.22 (Unix) mod_perl/1.26

Set-Cookie: MM_cookie=207.22.48.162.4731015262864476;

path=/; expires=Wed, 03-Mar-04 17:27:44 GMT;

domain=.adobe.com

Connection: close

Content-Type: text/html

Filecontent

File contents, for text and MIME files.

Mimetype

MIME type. Examples of MIME types include text/html, image/png, image/gif, video/mpeg, text/css, and audio/basic.

responseHeader

Response header. If there is one instance of a header key, this value can be accessed as a simple type. If there is more than one instance, values are put in an array in the responseHeader structure.

Statuscode

HTTP error code and associated error string. Common HTTP status codes returned in the response header include the following:

400: Bad Request

401: Unauthorized

403: Forbidden

404: Not Found

405: Method Not Allowed

Referencing HTTP Post parameters in the CF.http function

To pass HTTP Post parameters in the CF.http function, construct an array of objects and assign this array to a variable named params. The following arguments can only be passed as an array of objects in the params argument of the CF.http function:

Parameter

Description

name

The variable name for data that is passed

type

Transaction type:

  • URL

  • FormField

  • Cookie

  • CGI

  • File

value

Value of URL, FormField, Cookie, File, or CGI variables that are passed

In the following example, the CF.http function passes HTTP Post parameters in an array of objects:

function postWithParamsAndUser() 
{ 
    // Set up the array of Post parameters. These are just like cfhttpparam tags. 
    params = new Array(); 
    params[1] = {name:"arg2", type:"URL", value:"value2"}; 
 
    url = "http://localhost:8500/"; 
 
    // Invoke with the method, url, params, username, and password 
    result = CF.http("post", url, params, "karl", "salsa"); 
    return result.get("Filecontent"); 
}

Using the CF.http Post method

You use the Post method to send cookie, form field, CGI, URL, and file variables to a specified ColdFusion page or CGI program for processing. For POST operations, use the params argument for each variable that you post. The Post method passes data to a specified ColdFusion page or an executable that interprets the variables being sent, and returns data.

For example, when you build an HTML form using the Post method, you specify the name of the page to which form data is passed. You use the Post method in the CF.http function in a similar way. However, with the CF.http function, the page that receives the Post does not display anything. See the following example:

function postWithParams() 
{ 
    // Set up the array of Post parameters. These are just like cfhttpparam tags. 
    // This example passes formfield data to a specified URL. 
    params = new Array(); 
    params[1] = {name:"Formfield1", type:"FormField", value:"George"}; 
    params[2] = [name:"Formfield2", type:"FormField", value:"Brown"}; 
 
    url = "http://localhost:8500/"; 
 
    // Invoke CF.http with the method, url, and params 
    result = CF.http("post", url, params); 
    return result.get("Filecontent"); 
}

Using the CF.http Get method

You use the Get method to retrieve files, including text and binary files, from a specified server. You reference properties of the object returned by the CF.http function to access things like file content, header information, MIME type, and so on.

The following example uses the CF.http function to show a common approach to retrieving data from the web:

// Returns content of URL defined in url variable 
// This example uses positional argument style 
function get() 
{ 
    url = "http://www.adobe.com/software/coldfusion/"; 
 
    //Invoke with just the url argument. Get is the default. 
    result = CF.http(url); 
    return result.get("Filecontent"); 
}

For more information about CF.http function properties, see CF.http in the CFML Reference.