Using the cfhttp Post Method

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.

To pass variables to a ColdFusion page:

  1. Open a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    
    <html>
    
    <head>
    
      <title>HTTP Post Test</title>
    
    </head>
    
    
    
    <body>
    
    <H1>HTTP Post Test</H1>
    
    
    
    <cfhttp method="Post"
    
      url="http://127.0.0.1/myapps/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="wbfreuh@macromedia.com"
    
        name="emailaddress">
    
      <cfhttpparam type="File"
    
        name="myfile"
    
        file="c:\testImage.gif">
    
    </cfhttp>
    
    
    
    <cfoutput>
    
    File Content:<br>
    
      #cfhttp.filecontent#<br>
    
    <br>
    
    Mime Type:  #cfhttp.MimeType#<br>
    
    </cfoutput>
    
    </body>
    
    </html>
    
    
  3. Replace the path to the GIF file to a path on your server.
  4. Save the file as posttest.cfm in myapps under your Web root directory.

Reviewing the code

The following table describes the code and its function:
Code
Description

<cfhttp method="Post" 

  url="http://127.0.0.1/myapps/

  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="wbfreuh@macromedia.com" 

  name="emailaddress"> 

Send a Form field in the request.

  <cfhttpparam type="File" 

    name="myfile" 

    file="c:\testImage.gif"> 

</cfhttp> 

Send a file in the request.
The </cfhttp> tag ends the http request.

<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 cfoutput tag in server.cfm.

Mime Type:  #cfhttp.MimeType#<br> 

</cfoutput> 

Display the MIME type of the created file.

To view the variables:

  1. Create a new file in ColdFusion Studio.
  2. Modify the file so that it appears as follows:
    <cffile destination="C:\temp\Junk"
    
      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>
    
    
  3. Replace C:\temp\Junk with an appropriate directory path on your hard drive.
  4. Save the file as server.cfm in myapps under your Web root directory.
  5. View posttest.cfm in your browser and look for the file in C:\temp\Junk (or your replacement path).

Reviewing the code

The following table describes the code and its function:
Code
Description

<cffile destination="C:\temp\Junk" 

  nameconflict="Overwrite" 

  filefield="Form.myfile" 

  action="Upload" 

  attributes="Normal"> 

Write the transferred document to a file on the server. Note that you send the file using the cfhttpparam type="File" attribute, but the receiving page gets it as a Form variable, not a File variable. This cffile tag creates File variables, as follows.

<cfoutput> 

Output information. The results are not displayed by this page. They are passed back to the posting page in its cfhttp.filecontent variable.

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:

#Form.emailaddress#. <br> 

Output the Form variable sent in the HTTP request. Note that you send the variable using the type="formField" attribute but the receiving page gets it as a Form variable.

The file was uploaded to

#File.ServerDirectory#\#File.

ServerFile#. 

</cfoutput> 

Output the results of the cffile tag on this page. This time, the variables really are File variables.

To return results of a CGI program:

The following code runs the (theoretical) CGI program search.exe on the (equally theoretical) somesiteorother.com site 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.somesiteorother.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>



Banner.Novgorod.Ru