Posting Form Values With The ColdFusion CFHttp And CFHttpParam Tags

Posted November 1, 2006 at 7:29 AM by Ben Nadel

Tags: ColdFusion

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:

  • <!--- Set up response string buffer. --->
  • <cfset REQUEST.Response = CreateObject(
  • "java",
  • "java.lang.StringBuffer"
  • ).Init()
  • />
  •  
  • <!--- Loop over form collection and add to response string. --->
  • <cfloop item="strKey" collection="#FORM#">
  •  
  • <!--- Add key/value pair to response. --->
  • <cfset REQUEST.Response.Append(
  • IIF(
  • REQUEST.Response.Length(),
  • DE( "||" ),
  • DE( "" )
  • ) &
  • strKey & "=" &
  • FORM[ strKey ]
  • ) />
  •  
  • </cfloop>
  •  
  • <!---
  • Reset output and write response to page. Then, abort out
  • of page so that we ensure no other data is written to the
  • reponse stream.
  • --->
  • <cfcontent
  • type="text/plain"
  • reset="true"
  •  
  • /><cfset WriteOutput( REQUEST.Response.ToString() )
  •  
  • /><cfabort />

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:

  • <!--- Determine the URL to post to. --->
  • <cfset strUrl = (
  • "http://" &
  • CGI.http_host &
  • GetDirectoryFromPath( CGI.script_name ) &
  • "post.cfm"
  • ) />
  •  
  •  
  • <!--- POST data to url using CFHttp. --->
  • <cfhttp
  • url="#strUrl#"
  • method="POST"
  • result="objHttp">
  •  
  • <cfhttpparam
  • type="FORMFIELD"
  • name="last_name"
  • value="nadel"
  • />
  •  
  • <cfhttpparam
  • type="FORMFIELD"
  • name="first_name"
  • value="ben"
  • />
  •  
  • </cfhttp>
  •  
  • <!--- Output reponse string. --->
  • <cfset WriteOutput(
  • objHttp.FileContent
  • ) />

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.




Reader Comments

Nov 1, 2006 at 3:48 PM // reply »
18 Comments

Ben,

Take a look at aSyncHttp...

http://www.compoundtheory.com/?action=asynchttp.index

Sami


Nov 1, 2006 at 4:14 PM // reply »
11,238 Comments

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.


Nov 2, 2006 at 2:51 PM // reply »
16 Comments

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>


Jan 23, 2009 at 9:33 PM // reply »
2 Comments

Sorry to post on such an old post...but, I'm trying to cfhttp post to a java struts app on en external server. They need lowercase fieldnames and cfhttp sends uppercase fieldnames. Are you aware of a 'fix' for this? I've try everything I'd consider a trick and I'm almost convinced it can't be done. Let me know if you've run into this. Thanks!


Feb 6, 2009 at 9:32 AM // reply »
11,238 Comments

@Dallas,

Have you tried putting an LCase() around the name of the fields?

<cfhttpparam type="formfield" name="#LCase( ... )#" value="" />


Feb 6, 2009 at 9:50 AM // reply »
2 Comments

@Ben, Thanks for the reply. I tried Lcase(), but it turns out that the cert was throwing errors as it wasn't in trusted root. The Strut developer on the vendor side kept telling me I was passing uppercase vars. Anyway, found the problem, fixed by importing cert. Thanks again.


Feb 6, 2009 at 9:56 AM // reply »
11,238 Comments

@Dallas,

Ok, good to hear.


Jul 7, 2009 at 8:31 AM // reply »
1 Comments

We have a issue while making http request(method=post). When we make a http request to a page.Most of the time we get desired result.

If desired response is shown , we receive the below header and response content from the page requested.

Header:
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Connection: close Date: Tue, 07 Jul 2009 11:01:37 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET
Charset: utf-8

If we don't get a desired repsonse, we receive the below header and response content from the page requested

Header:
HTTP/1.1 200 OK Connection: close Expires: Thu, 01 Jan 1970 00:00:00 GMT Content-Language: en-US Date: Tue, 07 Jul 2009 11:47:21 GMT Server: IBM_HTTP_Server/6.1.0.21 Apache/2.0.47 Pragma: no-cache Cache-Control: no-cache Content-Type: text/html; charset=ISO-8859-1

Charset: ISO-8859-1

Can you please help me , why get two types of headers and reponse for a single http request to page ?


Jul 7, 2009 at 9:24 AM // reply »
11,238 Comments

@Shivakarthikeyan,

I am not sure why the headers would change. Do these headers actually change the way your calling page functions?


Oct 3, 2011 at 4:19 PM // reply »
16 Comments

Ben,
What if you're doing this but submitting to a form validated with JS? I'm trying to log into a site where the Submit (login) button calls a JS function, which on further examination is passing JSON data. I just keep getting the log in page returned. I've tried with putting the username and password in the cfhttp request, as well as using POST and cfhttpparam elements. Shouldn't submitting the form using POST take care of all that?

Once I get into the site there's a ton of javascript and broken up pages. Do you know if CFHTTP would work with that? I have to imagine it would, but I'm thus far quite unfamiliar with it.

Thank you!


Feb 21, 2012 at 1:08 AM // reply »
22 Comments

Has anyone noticed that if you try to post a string that exceeds 1,000,000 characters, it simply does not include the field with the request? ..and doesn't throw!

eg.
<cfscript>
var h = new http( url = "http://...", method = "post" );
h.addParam( type = "formField", name = "a", value = repeatString("a",5000) );
h.addParam( type = "formField", name = "b", value = repeatString("b",1000000) );
h.addParam( type = "formField", name = "c", value = repeatString("c",1000001) );
var p = h.send().getPrefix();
writeDump( var = p, abort = true );
</cfscript>

The "a" and "b" fields are present in the form scope of the recipient page. The "c" field is missing!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools