CF.htttp

Description

Executes HTTP POST and GET operations on files. POST operations upload MIME file types to a server, or post cookie, formfield, URL, file, or CGI variables directly to a server.

Return value

Returns an object containing properties that you reference to access data. For available properties returned by the CF.http function, see "Properties available with the CF.http object".

Syntax

CF.http
  ({
    method:"get or post",
    url:"URL",
    username:"username",
    password:"password",
    resolveurl:"yes or no",
    params:arrayvar,
    path:"path",
    file:"filename"
  })

Arguments

Arguments
Req/Opt
Description
method
Required
Two arguments are supported:
  • get: downloads a text or binary file or creates a query from the contents of a text file.
  • post: sends information to the server page or CGI program for processing. Requires the params argument.
url
Required
The absolute URL of the host name or IP address of server on which the file resides. The URL must include the protocol (http or https) and host name.
username
Optional
When required by a server, a username.
password
Optional
When required by a server, a password.
resolveurl
Optional
For Get and Post methods.
  • Yes or No. Default is No.
For GET and POST operations, if Yes, page reference that is returned into the Filecontent property has its internal URLs fully resolved, including port number, so that links remain intact. The following HTML tags, which can contain links, are resolved:
  • - img src
  • - a href
  • - form action
  • - applet code
  • - script src
  • - embed src
  • - embed pluginspace
  • - body background
  • - frame src
  • - bgsound src
  • - object data
  • - object classid
  • - object codebase
  • - object usemap
params
Optional
HTTP parameters passed as an array of objects. Supports the following parameter types:
  • name
  • type
  • value
CF.http params are passed as an array of objects. The params argument is required for POST operations.
For detailed information about each params argument, see "CF.http parameters" .
path
Optional
The path to the directory in which to store files. When using the path argument, the file argument is required.
file
Optional
Name of the file that is accessed. For GET operations, defaults to name specified in the url argument. Enter path information in the path attribute.

Usage

You can code the CF.http function using named arguments or positional arguments. You can invoke all supported arguments using the named argument style, as follows:

CF.http({method:"method", url:"URL", username:"username", password:"password",
  resolveurl:"yes or no", params:arrayvar, 
  path:"path", file:"filename"});

Note:   The named argument style uses curly braces {} to surround the function arguments.

Positional arguments let you use a shorthand coding style. However, not all arguments are supported for the positional argument style. Use the following schemas to code the CF.http function using positional arguments:

CF.http(url);
CF.http(method, url);
CF.http(method, url, username, password);
CF.http(method, url, params, username, password);

Note:   When using positional arguments, do not use curly braces {}.

CF.http parameters

The following parameters can only be passed as an array of objects in the params argument in the CF.http function:

Parameter
Description
name
The variable name for data that is passed
type
The transaction type:
  • URL
  • FormField
  • Cookie
  • CGI
  • File
value
Value of URL, FormField, Cookie, File, or CGI variables that are passed

Properties available with the CF.http object

The CF.http function returns data as a set of object properties:
Property
Description
Text
A Boolean value that indicates whether the specfied 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. For example, macromedia.com returns the following 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=.macromedia.com
Connection: close
Content-Type: text/html
Filecontent
File contents, for text and MIME files.
Mimetype
MIME type. Examples of content 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, value can be accessed as simple type. If there is more than one instance, values are put in an array in responseHeader structure.
Statuscode
HTTP error code and associated error string. Common HTTP status codes returned in the response header include:
400: Bad Request
401: Unauthorized
403: Forbidden
404: Not Found
405: Method Not Allowed

You access these attributes using the get function:

function basicGet()
{
    url = "http://localhost:8100/";
 
    // Invoke with just the url. This is an http get.
    result = CF.http(url);
    return result.get("Filecontent");
}

Example

The following examples show a number of the ways to use the CF.http function:

function postWithNamedArgs()
{
  // Set up the array of post parameters.
  params = new Array();
  params[1] = {name:"arg1", type:"FormField", value:"value1"};
  params[2] = {name:"arg2", type:"URL", value:"value2"};
  params[3] = {name:"arg3", type:"CGI", value:"value3"};

  url = "http://localhost:8100/";

  path = application.getContext("/").getRealPath("/");
  file = "foo.txt";

  result = CF.http({method:"post", url:url, username:"karl", password:"salsa",
resolveurl:true, params:params, path:path, file:file});

  if (result)
    return result.get("Statuscode");
  return null;
}

// Example of a basic HTTP get operation
// Shows that HTTP Get is the default
function basicGet()
{
  url = "http://localhost:8100/";

  // Invoke with just the URL. This is an HTTP Get.
  result = CF.http(url);
  return result.get("Filecontent");
}

// Example showing simple array created to pass params arguments
function postWithParams()
{
  // 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:8100/";

  // Invoke with the method, url, and params
  result = CF.http("post", url, params);
  return result.get("Filecontent");
}

// Example with username and params arguments
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:8100/";

  // Invoke with the method, url, params, username, and password
  result = CF.http("post", url, params, "karl", "salsa");
  return result.get("Filecontent");
}

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.