Ask Ben: Posting XML And File Data With ColdFusion And CFHttp

Posted December 4, 2008 at 12:25 PM

Tags: ColdFusion, Ask Ben

I am using CFHTTP to send a multipart message XML and an image file (fax tiff), the question I have is how do you send a XML type and File type in the same post? I have tried using cfhttp type designator, but cold fusion errors saying that you can not have both a XML and File type.

The problem here is that you are using the ColdFusion CFHttpParam type of "XML". If you use CFHttpParam type of "XML" or "Body", you cannot mix in other pieces of data because XML and Body are designed to be the only data going across (the entire body of the message). In fact, if you designate the type, XML, behind the scenes, it is actually using type, Body, and setting the mime type to be "text/xml". As such, the XML type is merely a short-hand for the type Body.

If you try to mix and match, you will get the following ColdFusion error (as you have probably seen):

You cannot mix the use of cfhttpparam tags of type file or formfield with cfhttpparam tags of type body or XML.

This is not a problem, however; all we need to do is not use type XML or Body when posting multiple pieces of information. If we need to POST some XML and a file, let's just treat this like it's a standard form post and post each of these datum as if they were form data:

 Launch code in new window » Download code as text file »

  • <!--- Define our generic CFHTTP properties. --->
  • <cfset objCFHttpProperties = {
  • Useragent = "#CGI.http_user_agent#",
  • Username = "xxxxxxx",
  • Password = "yyyyyyy"
  • } />
  •  
  •  
  • <!---
  • Get the URL for our post. Since CFHttp requires a full
  • URL, not just a relative one, we need to build it.
  • --->
  • <cfset strURL = (
  • CGI.server_name &
  • GetDirectoryFromPath( CGI.script_name ) &
  • "cfhttp_catch.cfm"
  • ) />
  •  
  •  
  • <!--- Create the XML data to post. --->
  • <cfxml variable="xmlPurchaseData">
  •  
  • <transaction>
  • <type>Debit</type>
  • <cost>19.95</cost>
  • <date>12-04-2008</date>
  • <item>
  • <sku>BTC-CANADA-07189</sku>
  • <name>Better Than Chocolate</name>
  • </item>
  • </transaction>
  •  
  • </cfxml>
  •  
  •  
  • <!--- Create the file path to the DVD cover. --->
  • <cfset strFilePath = ExpandPath( "./cover.jpg" ) />
  •  
  •  
  • <!---
  • Now, we are going to post the XML data along with the
  • cover of the movie that we are pretending to purchase.
  • --->
  • <cfhttp
  • url="#strURL#"
  • method="POST"
  • result="objGet"
  • attributecollection="#objCFHttpProperties#">
  •  
  • <!---
  • When posting the XML data, ColdFusion will worry
  • about converting the XML document back into a string.
  • --->
  • <cfhttpparam
  • type="formfield"
  • name="purchase_data"
  • value="#xmlPurchaseData#"
  • />
  •  
  • <!--- Post the file. --->
  • <cfhttpparam
  • type="file"
  • name="cover"
  • file="#strFilePath#"
  • mimetype="image/jpeg"
  • />
  •  
  • </cfhttp>
  •  
  •  
  • <!--- Output the return message. --->
  • Message: #objGet.FileContent#

As you can see here, we are performing a FORM POST. The Xml data is being send over as a form field and the file is being sent across as a file (specialized form field). Nothing special going on.

This data is being posted to the following "catch" page:

 Launch code in new window » Download code as text file »

  • <!--- Param the form fields. --->
  • <cfparam name="FORM.purchase_data" type="string" default="" />
  • <cfparam name="FORM.cover" type="string" default="" />
  •  
  •  
  • <!--- Write the purchase data to file. --->
  • <cffile
  • action="write"
  • file="#ExpandPath( './purchase_data.xml' )#"
  • output="#FORM.purchase_data#"
  • />
  •  
  • <!--- Upload the DVD cover. --->
  • <cffile
  • action="upload"
  • filefield="cover"
  • destination="#ExpandPath( './' )#"
  • nameconflict="makeunique"
  • />
  •  
  •  
  • <!--- Return response string. --->
  • <cfcontent
  • type="text/plain"
  • variable="#ToBinary( ToBase64( 'Processing Done!' ))#"
  • />

This "catch" page looks and acts just like any standard ColdFusion form processing page. We are treating the posted data as if they were FORM-scoped data because, in fact, that is what they are. Once we accept this, we can then process the form data as we would normally (here I am saving the XML and image to file).

I hope this helps.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Dec 4, 2008 at 12:45 PM // reply »
207 Comments

The problem I have with your solution is that sometimes the receiver of the data requires you to post the data as a multipart http post. Splitting it up into two form fields only works if you have control over the receiver.

As an example, when I had to do my file uploads for youtube (http://youtubecfc.riaforge.org), I had to do both an XML field and a file field. This is the code I used:

<cfhttp url="#theurl#" method="post" result="result" multiparttype="related">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#variables.authtoken#">
<cfhttpparam type="header" name="X-GData-Client" value="youtubecfc">
<cfhttpparam type="header" name="X-GData-Key" value="key=#variables.devkey#">
<cfhttpparam type="header" name="Slug" value="#listLast(arguments.video,"\/")#">
<cfhttpparam type="file" name="API_XML_Request" file="#tmpfile#" mimetype="application/atom+xml; charset=UTF-8 ">
<cfhttpparam type="file" name="file" file="#arguments.video#" mimetype="video/*">
</cfhttp>

Notice the XML is sent as a file (only sucky part, I had to save the XML string to the file system) and includes a mimetype.

Note - this code makes use of the new multipart=related argument that was added per a hotfix for cf7 and 8. Ie, this is not part of the documented CF language, although it is "official" if you get the hotfix.


Dec 4, 2008 at 12:51 PM // reply »
6,516 Comments

@Ray,

Interesting. I guess I have never had to do a real "multi-part" post. I just thought the Asker meant that he was trying to submit several parts of data. I am assuming he was trying to post XML and File types (which is why he was getting an errror).

This is good to know though - I have never come up against this, so I am sure it will save me headache when I do. Thanks!


Dec 4, 2008 at 1:01 PM // reply »
207 Comments

Don't get me started on Google and their freaking APIs. (Since Google owns YouTube now they had to make the api 'better')


Dec 4, 2008 at 1:04 PM // reply »
6,516 Comments

@Ray,

Ha ha, breathe breathe :)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »
Nov 20, 2009 at 5:07 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, I have seen tidbits about the way Railo handles session. I can understand that it lazy-loads sessions, but I also think that I might make some things more complicated. For example, often tim ... read »