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, you must use the cfhttpparam
tag for each variable you want to 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 cfhttp
in a similar way. However, with cfhttp
, the page that receives the Post does not, itself, display anything.
<html> <head> <title>HTTP Post Test</title> </head> <body> <h1>HTTP Post Test</h1> <cfhttp method="Post" url="http://127.0.0.1:8500/myapps/post_test_server.cfm"> <cfhttpparam type="Cookie" value="cookiemonster" name="mycookie6"> <cfhttpparam type="CGI" value="cgivar " name="mycgi"> <cfhttpparam type="URL" value="theurl" name="myurl"> <cfhttpparam type="Formfield" value="[email protected]" name="emailaddress"> <cfhttpparam type="File" name="myfile" file="c:\pix\trees.gif"> </cfhttp> <cfoutput> File Content:<br> #cfhttp.filecontent#<br> Mime Type: #cfhttp.MimeType#<br> </cfoutput> </body> </html>
cfhttp
tag).
Note: You must write a page to view the variables. This is the next procedure.
The following table describes the code and its function:
Code | Description |
---|---|
<cfhttp method="Post" url="http://127.0.0.1:8500/myapps/post_test_ server.cfm"> |
Post an HTTP request to the specified page. |
<cfhttpparam type="Cookie" value="cookiemonster" name="mycookie6"> |
Send a cookie in the request. |
<cfhttpparam type="CGI" value="cgivar " name="mycgi"> |
Send a CGI variable in the request. |
<cfhttpparam type="URL" value="theurl" name="myurl"> |
Send a URL in the request. |
<cfhttpparam type="Formfield" value="[email protected]" name="emailaddress"> |
Send a Form field in the request. |
<cfhttpparam type="File" name="myfile" file="c"\pix\trees.gif"> |
Send a file in the request. The |
<cfoutput> File Content:<br> #cfhttp.filecontent#<br> |
Display the contents of the file that the page that is posted to creates by processing the request. In this example, this is the output from the |
Mime Type: #cfhttp.MimeType#<br> </cfoutput> |
Display the MIME type of the created file. |
<html> <head><title>HTTP Post Test</title> </head> <body> <h1>HTTP Post Test</h1> <cffile destination="C:\temp\" nameconflict="Overwrite" filefield="Form.myfile" action="Upload" attributes="Normal"> <cfoutput> The URL variable is: #URL.myurl# <br> The Cookie variable is: #Cookie.mycookie6# <br> The CGI variable is: #CGI.mycgi#. <br> The Formfield variable is: #Form.emailaddress#. <br> The file was uploaded to #File.ServerDirectory#\#File.ServerFile#. </cfoutput> </body> </html>
The following table describes the code and its function:
Code | Description |
---|---|
<cffile destination="C:\temp\" nameconflict="Overwrite" filefield="Form.myfile" action="Upload" attributes="Normal"> |
Write the transferred document to a file on the server. You send the file using the |
<cfoutput> |
Output information. The results are not displayed by this page. They are passed back to the posting page in its |
The URL variable is: #URL.myurl# <br> |
Output the value of the URL variable sent in the HTTP request. |
The Cookie variable is: #Cookie.mycookie# <br> |
Output the value of the Cookie variable sent in the HTTP request. |
The CGI variable is: #CGI.mycgi# <br> |
Output the value of the CGI variable sent in the HTTP request. |
The Form variable is: |
Output the Form variable sent in the HTTP request. You send the variable using the |
The file was uploaded to #File.ServerDirectory#\#File. |
Output the results of the |
The following code runs a CGI program search.exe on a website and displays the results, including both the MIME type and length of the response. The search.exe program must expect a "search" parameter.
<cfhttp method="Post" url="http://www.my_favorite_site.com/search.exe" resolveurl="Yes"> <cfhttpparam type="Formfield" name="search" value="Macromedia ColdFusion"> </cfhttp> <cfoutput> Response Mime Type: #cfhttp.MimeType#<br> Response Length: #len(cfhttp.filecontent)# <br> Response Content: <br> #htmlcodeformat(cfhttp.filecontent)#<br> </cfoutput>