Skip to main content
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Paul James
Ben Nadel at cf.Objective() 2011 (Minneapolis, MN) with: Paul James

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

Published in , Comments (7)

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:

<!--- 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 ) &
	) />

<!--- Create the XML data to post. --->
<cfxml variable="xmlPurchaseData">

			<name>Better Than Chocolate</name>


<!--- 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.

		When posting the XML data, ColdFusion will worry
		about converting the XML document back into a string.

	<!--- Post the file. --->


<!--- 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:

<!--- 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. --->
	file="#ExpandPath( './purchase_data.xml' )#"

<!--- Upload the DVD cover. --->
	destination="#ExpandPath( './' )#"

<!--- Return response string. --->
	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.

Want to use code from this post? Check out the license.

Reader 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 (, 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(,"\/")#">
<cfhttpparam type="file" name="API_XML_Request" file="#tmpfile#" mimetype="application/atom+xml; charset=UTF-8 ">
<cfhttpparam type="file" name="file" file="" mimetype="video/*">

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.



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!



Glad you got it working. I've yet to experiment with the multipartType yet. Thanks for the reminder that this was out there.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel