View comments | RSS feed

cfhttp

Description

Executes HTTP POST and GET operations on files. You can execute standard GET operations and create a query object from a text file. POST operations upload MIME file types to a server, or post cookie, formfield, URL, file, or CGI variables directly to a server.

Category

Forms tags, Internet Protocol tags

Syntax

<cfhttp 
  url = "hostname"
  port = "port_number"
  method = "get_or_post"
  username = "username"
  password = "password"
  name = "queryname"
  columns = "query_columns"
  firstrowasheaders = "yes" or "no" 
  path = "path"
  file = "filename"
  delimiter = "character"
  textQualifier = "character"
  resolveURL = "yes" or "no"
  proxyServer = "hostname"
  proxyPort = "port_number"
  userAgent = "user_agent"
  throwOnError = "yes" or "no"
  redirect = "yes" or "no"
  timeout = "timeout_period"
  charset = "character set">
</cfhttp>

See also

cfftp, cfhttpparam, cfldap, cfmail, cfmailparam, cfpop

History

New in ColdFusion MX:

Attributes

Attribute Req/Opt Default Description
url
Required
http
Absolute URL of host name or IP address of server on which file resides. URL must include protocol (http or https) and hostname. It can contain a port number. This value overrides port value.
port
Optional
80
Port number on server from which object is requested. When used with resolveURL, URLs of retrieved documents that specify a port number are automatically resolved, so that links in retrieved document remain functional. Value in url attribute overrides this value.
method
Optional
get
  • get: downloads text or binary file, or create query from the contents of a text file.
  • post: sends information to server page or CGI program for processing. Requires a cfhttpparam tag.
username
Optional

A username. May be required by server.
password
Optional

A password. May be required by server
name
Optional

Name to assign to a query if it is constructed from returned HTTP results.
columns
Optional

Column names for a query, when creating it as a result of a cfhttp method = "get".
By default, the first row of a text file is interpreted as column heads. If there are column heads in the text file from which the query is drawn, do not specify this attribute, except to overwrite them.
If a duplicate column heading is encountered, ColdFusion appends an underscore to the name to make it unique.
If there are no column headers in the text file, or to override those in the file, specify the columns attribute. ColdFusion never processes the first row of a file as data,.
firstrowasheaders
Optional
yes
Determines how ColdFusion processes the first row of the query record set:
  • yes:
  • - If the columns attribute is not specified: processes as column heads
  • - If the columns attribute is specified: this attribute is not used
  • no:
  • - If the columns attribute is not specified: processes as data, and generates column names; for example, "column_1"
  • - If the columns attribute is specified: this attribute is not used
path
Optional

Path to directory in which to store file. If path is not specified in POST or GET, a variable (cfhttp.fileContent) is created; you can use it to display the post results, in a cfoutput tag.
file
Required if post and if path is specified

Name of file that is accessed. For a get operation, defaults to name specified in url. Enter path information in the path attribute.
delimiter
Required to create query
, [comma]
  • tab
  • comma
textQualifier
Required to create query
"" [double quotation mark]
Start and end of a column. If it is embedded in column, this value must be escaped. For example, if qualifier is a double quotation mark, it is escaped as """". If there is no text qualifier in the file, specify " ".
resolveURL
Optional
No
Applies to get and post.
  • Yes: internal URLs in a page reference returned into fileContent internal variable are fully resolved, including port number, so that links in the page remain functional. Applies to these HTML tags:
  • - 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
proxyServer
Optional

Host name or IP address of a proxy server.
proxyPort
Optional
80
Port number on proxy server from which object is requested. When used with resolveURL, the URLs of retrieved documents that specify a port number are automatically resolved, so that links in the retrieved document remain functional.
userAgent
Optional

User agent request header.
throwOnError
Optional
No
  • Yes: throw an exception that can be caught with the cftry and cfcatch tags
  • No
redirect
Optional
Yes
If No, and throwOnError = "yes": if cfhttp fails, execution stops, and the status code and associated error message are returned in the variable cfhttp.statuscode.
To determine where execution would be redirected, use the variable cfhttp.responseHeader[LOCATION]. The key LOCATION shows the redirection path. ColdFusion follows a maximum of four redirects on a request. If there are more, ColdFusion functions as if redirect = "no".
  • Yes: redirect execution
  • No: stop execution
timeout
Optional

Value, in seconds. When a URL timeout is specified in the browser, this setting takes precedence over the ColdFusion Administrator timeout, and ColdFusion uses the lesser of the URL timeout and the timeout passed in the timeout attribute, so that the request always times out before, or at the same time as, the page.
If URL timeout is not specified, ColdFusion uses the lesser of the Administrator timeout and the timeout passed in the timeout attribute.
If the timeout is not set in any of these, ColdFusion waits indefinitely for the cfhttp request to process.
This attribute does not function with JDK 1.3.
charset
Optional
The server charset; if none, UTF-8
A Java character set name for the file or url in a GET or POST. The following values are typically used:
  • UTF-8
  • ISO-8859-1
  • UTF-16
  • US-ASCII
  • UTF-16BE
  • UTF-16LE
For a list of character sets, see:
http://www.w3.org/International/O-charset-lang.html

Usage

If this value includes the substring "https" (which indicates that the object of the request is a secure server) but does not include a port number, the default value of the port attribute is 443. (If the URL value includes a port number, its value overrides the value of port.)

For the ColdFusion Administrator timeout and the URL timeout to take effect, you must enable the timeout in the ColdFusion Administrator, Server Settings page. For more information, see Administering ColdFusion MX.

Variables returned by a cfhttp get operation

The cfhttp tag returns data in variables. For example, if you specify a URL that points to a text or binary file in a cfhttp method = "get" operation, the file is downloaded and stored in a ColdFusion variable or file.

These variables can be accessed in this way:

#cfhttp.fileContent# 
#cfhttp.mimeType#
#cfhttp.header#
#cfhttp.responseHeader[http_header_key]#

The responseHeader variable is returned as a CFML structure; the others, as strings.

Building a query from a delimited text file

To download a file in a ColdFusion page so that a query can be built using the file, the file must be either comma- or tab-delimited. Macromedia recommends that you include text qualification. The file is parsed and an appropriate query is built from it. You can specify columns in the attribute list so the client can override the columns specified in the file.

There is error checking within the tag that prevents a user from entering an invalid column name, or using an invalid column name that was specified in the original file. If an illegal filename is encountered, the illegal characters are stripped. This action could produce duplicate column names, so duplicate columns are renamed and inserted into the query header.

The query has all of the functionality of a standard cfquery object, as follows:

The cfhttp tag returns the following variables:
Name Description
#cfhttp.fileContent# 
File contents, for text and MIME files.
#cfhttp.mimeType#
MIME type.
#cfhttp.responseHeader[http_hd_key]#
Response headers. 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.
#cfhttp.header#
Raw response header.
#cfhttp.statuscode#
HTTP error code and associated error string, if throwOnError = "no".

Terminate a cfhttp method = "post" operation with </cfhttp>.

Example

<!--- This example shows the use of CFHTTP to pull information dynamically from 
the snippets directory ---> 
<p>This view-only example shows the ability of CFHTTP to pull the contents of 
a web resource from the Internet or from a local directory. 
<!--- 
<form action="index.cfm" method="POST"> 
Try entering a URL for the tag to return: 
  <input type="Text" size="25" name="urladdress" 
    value="http://www.macromedia.com"> 
  <input type="Submit" name="" value="get page"> 
</form> 
<!----- sets a default value for a URL to retrieve -----> 
<cfparam name="urladdress" 
  default="http://localhost/cfdocs/index.htm">
<!--- if we have passed a url address in the form, display the address ---> 
<cfif IsDefined("form.urladdress") is True> 
  <!--- do simple error check to avoid crashing the tag ---> 
  <cfif Trim(Form.urladdress) is "" 
    or Trim(Form.urladdress) is "http://"> 
    <!--- if error condition tripped, set alternative ---> 
    <cfset urlAddress ="http://localhost/cfdocs/index.htm"> 
    <h4>because you entered no url or an empty string, the tag will 
    return the following address: http://localhost/cfdocs/index.htm</h4> 
  <cfelse> 
    <!--- otherwise use address passed from form ---> 
    <cfset urlAddress = form.urladdress> 
  </cfif> 
  <!--- use CFHTTP tag to get the file content represented     by urladdress ---> 
  <cfhttp url="#urladdress#" 
    method="GET" resolveurl="YES"> 
  </cfhttp> 
<cfelse> 
  <!---   the first time through, retrieve a URL that we know exists ----> 
  <cfhttp url="http://localhost/cfdocs/index.htm" 
    method="GET" resolveurl="YES"> 
  </cfhttp> 
</cfif> 
<!----   Now, output the file, including the mimetype and content ------> 
<h3>Show the file</h3> 
<cfoutput> 
  <p>Your file was of type: #CFHTTP.MimeType#
  <p>#HTMLCodeFormat(CFHTTP.FileContent)# 
</cfoutput> ---> 

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.

Comments


fusebox_steve said on Jul 1, 2002 at 10:20 PM :
<cfhtmlhead> should have an end tag. It's very annoying to have to escape double quotes.
rbils@amkor.com said on Jul 16, 2002 at 3:41 AM :
In addition to the FIRSTROWASHEADERS attribute being broken in the release of CF MX, the documentation incorrectly states:

If FIRSTROWASHEADERS is No, and "If the columns attribute is specified: this attribute is not used"

This is not true. If the columns attribute is used, CF uses the list as the actual column headers for the query and treats the first row of the file as data. So, the attribute isn't ignored.
ctina said on Jul 22, 2002 at 2:24 PM :
Update from Macromedia, in response to fusebox_steve: I've entered your idea about an end tag for the HTMLHEAD tag into our enhancements suggestions list.
ctina said on Jul 22, 2002 at 7:06 PM :
Update from Macromedia, CFHTTP tag, TYPE attribute = "xml":
this attribute specifies that the contentis XML. If method = "post", the xml is sent to the posting page as text in the http response header.
alex.lake said on Sep 5, 2002 at 3:49 PM :
I found that the PORT attribute doesn't seem to work as advertised.

<CFHTTP URL="217.204.1.6" PORT="20010"....> failed

but

<CFHTTP URL="217.204.1.6:20010"....> worked

This represents a needless incompatibility with earlier versions of CF
rnielsen said on Sep 20, 2002 at 2:29 PM :
Here is another slight difference between CF5 and CFMX:

In ColdFusion MX, if the columns attribute is specified and firsrowasheaders = true (the default) there is an additional check for data integrity for all the rows of the retrieved file. CF5 will ignore the first row and perform no data checks against it but CFMX does. Hence MX will throw an 'Invalid Columns' or 'Invalid Number of Columns' exception if the first row of data file retrieved does not contain as many columns as specified in the columns attribute or contains invalid data.
carehart@systemanage said on Dec 31, 2002 at 3:19 PM :
More unfortunate news on the new firstrowasheaders attribute. The docs above say that if it's set to "no" then:

- If the columns attribute is not specified: processes as data, and generates column names; for example, "column_1"

That would suggest that CF would create some column names automatically, which would be cool when one has a very large number of columns in the file being read. But I just tried it and simply got the error "The column name "1" is invalid." because that was the first column of data in the first row.

It's as if it's ignoring the firstrowasheaders="no" attribute. That attribute is indeed working if I add a COLUMNS attribute and give it column names, but again the point is that one shouldn't have to according to the docs.
carehart@systemanage said on Jan 2, 2003 at 8:14 AM :
I forgot to add that there is a workaround one might use. If you know the number of columns in the file being read, you can just run a little loop to create the column names automatically, as in:

&lt;cfset colnames=""&gt;<br>
&lt;cfloop from="1" to="7" index="i"&gt;<br>
&nbsp; &lt;cfset colnames = listappend(colnames,"column_"&i)&gt;<br>
&lt;/cfloop&gt;<br>

Then use that created "colnames" value in the CFHTTP as COLUMNS="#colnames#". Unfortunately, you have to know exactly how many columns to use or a failure to specify the right amount will get an error when CF tries to read the file and matches the number it finds against the number of columns you generated. It would be nice if it just did itself as it seems it's supposed to.
carehart@systemanage said on Jan 2, 2003 at 8:15 AM :
I forgot to mention that there is a work-around until it's fixed. If you
know the number of columns in the file being read, you can just run a little
loop to create the column names automatically, as in:

<cfset colnames="">
<cfloop from="1" to="7" index="i">
<cfset colnames = listappend(colnames,"column_"&i)>
</cfloop>

Then use that created "colnames" value in the CFHTTP as
COLUMNS="#colnames#". Unfortunately, you have to know exactly how many
columns to use or a failure to specify the right amount will get an error
when CF tries to read the file and matches the number it finds against the
number of columns you generated. It would be nice if it just did itself as
it seems it's supposed to.
mesh said on Jan 14, 2003 at 11:15 AM :
here is how to simply retrieve the contents of a web page:

<cfhttp url="#feedURL#" throwonerror="yes" method="get" />

<cfoutput>#CFHTTP.fileContent#</cfoutput>
saro28 said on Mar 6, 2003 at 5:58 PM :
One of the Nightmare with CFHTTP is, it does not support proxy authentication. Would macromedia modify this tag to support Proxy authentication. Also SSL is not properly implemented.
alinear said on Jun 6, 2003 at 12:09 PM :
cfhttp.statuscode -- I believe 'throwonerror' has to be set to YES for this to attempt to work, and 'throwonerror' defaults to 'no'.

I am using a leased CFMX server on a shared server, so I cannot confirm this is not 'my fault'. But when I use cfhttp.statuscode in a catch like:

<CFTRY>
<CFHTTP METHOD="GET" URL=(anything) THROWONERROR="YES">
<CFCATCH TYPE="Any">
<CFOUTPUT>#CFHTTP.STATUSCODE#</CFOUTPUT>
</CFCATCH>
</CFTRY>
<CFOUTPUT>#CFHTTP.FILECONTENT#</CFOUTPUT>

...I get an error something about 'statuscode' not being an object of cfhttp!
hlichtin said on May 8, 2003 at 10:20 AM :
This is a code bug and has been entered in the bug tracker.
hlichtin said on May 8, 2003 at 10:23 AM :
My response of today was to Jedimaster. The proxy issues are also known.
jedimaster said on May 5, 2003 at 3:32 PM :
It seems as if redirect=yes is not the default. Someone reported an issue where his cfhttp did NOT redirect until he added redirect=yes. Can someone confirm this?
phantem said on May 19, 2003 at 6:03 PM :
Is there any way to use CFHTTP to grab a page that requires the existance of a cookie, but does not accept a POST request?

Since the cookies are sent in the header, shouldn't it be possible to submit them without having to use the POST method of CFHTTP?
TheoPeters said on Aug 12, 2003 at 3:11 PM :
Its just plain stupid that I have to use CFHTTP to turn a text file into a query object. I have to save text to a place that is visible to the Internet and then retrieve it through my own web server. This functionality should NEVER have been part of cfhttp. It should have been two different functions: 1) cfhttp can retrieve text from a web server. 2) queryobj = texttoquery( textstring) would convert ANY properly formatted text to a query.
jrunrandy said on Aug 14, 2003 at 11:20 AM :
TheoPeter: I have entered this as enhancement request 53279.
csteinola said on Sep 23, 2003 at 5:42 PM :
I have never understood just what the "redirect" attribute is supposed to do.
Near as I can tell, it's never worked (since 4.0). It never "redirects" anywhere (not in the common sense of the word), whether throwonerror = "yes" or not.

Can someone write clearer documentation for this attribute? Why, when, and how one would use it?

Ultimately, I'd like to be able to post data, and have the browser change location to the page I just posted to (much like a standard form post). I thought the new "HEADER" type for the HTTPPARAM tag would handle this (so you could pass a "Location" header)... but it doesn't seem to... or there's absolutely no documentation on how to make it so.
halL said on Sep 24, 2003 at 7:23 AM :
The header attribute was added with ColdFusion MX 6.1.
This page documents ColdFusion MX 6.0. For 6.1 documentation, see http://livedocs.macromedia.com/coldfusion/6.1/.
The 6.1 version of this page also has a more complete description of the redirect attribute.
No screen name said on Sep 29, 2003 at 4:54 AM :
I want to put two enhancement
request for CFHTTP. Firstly, for
well-delimited files, the delimiters
should enclude pipe and space.
At least pipe (|) is one of the well known
delimeter. Secondly, it should be
able to deal with fixed-length files.
By using CFMX, we have lost the capability
to query the fixed length files, since it
does not suppot dynamic query any more.
jrunrandy said on Oct 24, 2003 at 9:17 AM :
ColdFusion MX 6.1 provides more flexibility with regard to delimiters. See http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p58.htm#wp1632966 for details.
No screen name said on Jun 15, 2004 at 10:17 AM :
THese codes work in CF5 but not MX..anyone know why? Does MX still support query by name?


<cfhttp url="http://127.0.0.1/johntest/allprinters.txt" name="info" method="get" textqualifier="">

<cfdump var="#info#">

<cfquery dbtype="query" name="Results">
SELECT printer FROM info group by printer
</cfquery>
</cfhttp>
qwerqwerqwe said on Oct 18, 2004 at 8:54 AM :
If the site that you are CFHHTPing to is using SSL that has not been delivered by a trusted CA then the connection will fail. CF basicly gets the little confirmation box that says something like, are you sure you want to accpet this certificate? But CFMX cannot click yes there. I suppose this is alot like the Secure LDAP settings in CFMX where we will have to configure the cert in the cacerts cert store in order for it to be trusted. I have very rarely gotten this to work. There is very little information on the cacert store and how we can attain certs that will work once they are loaded into the cert store.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt154.htm