The POST Method
Besides giving you the ability to retrieve external Web pages, you can use CFHTTP to POST data to a Web site. This data can include form, URL, or CGI variables, or cookies. Using CFHTTPPARAMs inside your CFHTTP operation, you could create a gateway that requires the presence of a cookie or username and password for your site partners. Once a partner has paid for your service, you could set up a form that accepts a username and password or product serial number. You could then create an agent that posts this information to retrieve the protected resources. Let's take a look at this new tag for a moment.
CFHTTPPARAM
The <CFHTTPPARAM> tag is required for POST operations of the CFHTTP tag. You use it to specify the parameters that you want to post. It is a subtag of CFHTTP, like CFGRAPHDATA is a subtag of CFGRAPH. It is therefore used only inside a CFHTTP call. Table 3 shows the attributes of this tag.
Table 3 Attributes of the <CFHTTPPARAM> Tag
Attribute |
Description |
---|---|
Name |
Required. A variable name for the data being passed. |
Type |
Required. The transaction type. Valid entries are: URL: The server will receive the data as if it had been appended to the URL as a standard parameter. FormField: The server will interpret the data as if it had been entered into a form field. Cookie: The server will interpret the data if the browser had sent a cookie. CGI: The server will make the posted data available as a CGI environment variable. File: The server will receive the file as an uploaded file. |
Value |
Optional for type = "file". Specifies the value of the variable being passed. |
File |
Returns the HTTP error code and associated error string if throwOnError is NO. |
Using this new tag, you can post information to an external Web site and bypass usual navigation and forms. You could use these parameters to let users enter information into a form in the manner that the external site expects it and then post the data to the site via an HTTP call. (See Listing 3.)
Listing 3: Post.cfm
<!---post to the external site-------> <cfhttp method="POST" url="http://www.MyBigSite.com" resolveURL = "YES" > <!---post a variable called "SearchTerms", since this is what the site expects. -------> <cfhttpparam type="FORMFIELD" name="SearchTerms" value="ColdFusion"> <!----pass cookie so we don't get interrupted-------> <cfhttpparam type="COOKIE" name="IsMember" value="1"> </cfhttp> <!---output the variables created by CF-------> <cfoutput> Response Status Code: #cfhttp.statusCode# <br> Response MIME Type: #cfhttp.mimetype# <br> Response Length: #len(cfhttp.filecontent)# <br> Response Content: #HTMLCodeFormat(cfhttp.filecontent)# <br> </cfoutput>
In this article, you've seen how to create agents to automate certain processes for you. You can do a lot more with these tags, too. You must consider copyright issues when you use these tags, though. It is possible to use CFHTTP to retrieve, for instance, the weather forecast or a dynamic map from an external Web site, clean up the contents of the returned file using regular expression parsing, and include it on your own site. The Developer's Exchange has many such tags that show you how to do this. It's a great way to learn regular expressions if those are something that you're not comfortable with. However, many of these tags are intended for development purposes only; many Web sites charge for use of their news, maps, or other content, so it's important to observe this when implementing agents.