There was a thread on CF-Talk yesterday about getting data from a CFHttp call and then posting it to another page using the CFHttp tag. This got me thinking and I realized that I have never posted data with ColdFusion's CFHttp tag, only retrieved it. Is there any better time to learn than the now?
To try this out, I made a page that takes the FORM scope and created response string based on the form fields. It is basically echoing what was submitted:
Launch code in new window » Download code as text file »
As you can see, for each variable in the FORM object, I add the key / value pair to a double-pipe delimited list. The IIF() statement in the Append() method call is just to ensure that I don't prepend the double pipe unless there is already data in the response buffer (a very nice use of IIF() if I may say so myself).
Then, I created a page that posts form variables to the previously created page:
Launch code in new window » Download code as text file »
This gives me the output (I added the line breaks for readability):
FIELDNAMES=LAST_NAME,FIRST_NAME||
FIRST_NAME=ben||
LAST_NAME=nadel
Pretty cool stuff. It works just like they said it would. It even submits the FIELDNAMES variable. Cool beans.
Download Code Snippet ZIP File
Comments (3) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Getting The Requested URL From The Page Request Object (Servlet) Without Using CGI
GetPageContext.Include() DOES Parse Files And More (Thanks Charlie Arehart)
Ben,
Take a look at aSyncHttp...
http://www.compoundtheory.com/?action=asynchttp.index
Sami
Posted by Sami Hoda on Nov 1, 2006 at 3:48 PM
Sami,
Thanks for the link. I actually looked at that when I was learning about deleting scheduled tasks programmatically. It's cool that it exists, but it seems like a bit of an overkill for such a simple task. Why bring so much Java into it.
Frankly, the whole "Async" thing can be mimiced (mostly) by setting a timeout on the CFHttp tag to 1 second. Granted, it still has to wait one second before the CFHttp tag times out (without throwing an error), but this is pretty darn close to Async for most use cases.
But certainly for more complicated examples where you might be launching many many async tasks, the AsyncHTTP is probably the way to go.
Thanks.
Posted by Ben Nadel on Nov 1, 2006 at 4:14 PM
From: http://cfdj.sys-con.com/read/230515.htm
<cfhttp url="http://del.icio.us/rss/psenn" method="get">
<cfset RSSObj = xmlParse(cfhttp.filecontent)>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>del.icio.us</title>
</head>
<body>
<cfoutput>
<!---<cfloop collection="#RSSObj.xmlRoot.xmlChildren[1]#" item="I">
#I#<br />
</cfloop>--->
<ul>
<cfloop from="2" to="#ArrayLen(RSSObj.xmlRoot.xmlChildren)#" index="I">
<cfset AnchorText = RSSObj.xmlRoot.xmlChildren[I].title.xmltext>
<cfset Destination = RSSObj.xmlRoot.xmlChildren[I].link.xmltext>
<cfset title = "">
<cfif StructKeyExists(RSSObj.xmlRoot.xmlChildren[I],"description")>
<cfset title = RSSObj.xmlRoot.xmlChildren[I].description.xmltext>
</cfif>
<cftry>
<cfset title = RSSObj.xmlRoot.xmlChildren[I].description.xmltext>
<cfcatch type="any">
<cfset title = "">
</cfcatch>
</cftry>
<li><a href="#Destination#" title="#title#">#AnchorText#</a></li>
</cfloop>
</ul>
</cfoutput>
</body>
</html>
Posted by Phillip Senn on Nov 2, 2006 at 2:51 PM