A reader asked for help using Brightcove's API to upload a video using ColdFusion. While I do not plan on writing a full wrapper, or going into great detail, I thought a quick blog post on what he tried, and why it failed, could help others who plan on using the service.

Let's first look at his original code and a description of the problem he had.

<cfhttp url="http://api.brightcove.com/services/post" method="post" timeout="600" result="resultVar" multipart="true">

<cfhttpparam type="file" name="file" file="c:\com\brightcove\AT_T_test.mp4" />

<cfset jsonRequest = '{"method": "create_video", "params": {"video": {"name": "test", "shortDescription": "test"},"token": "blahblahblah.."}}'>

<cfhttpparam type="url" name="json" value="#jsonRequest#">

<cfhttpparam type="body" name="json" value="#jsonRequest#">

</cfhttp>

Even without knowing the API, you can see that it makes use of JSON to pass parameters about the upload. This code won't work at all because you can't mix a httpparam of type body with type file. The API docs though seemed to imply you needed to send the JSON values as the body. I first attempted to simply save the JSON to the file system and send it as a second file. This didn't work. I then dig a bit of digging and discovered this forum post by the Brightcove team: http://forum.brightcove.com/brgh/board/message?board.id=Developer_2&message.id=85

It pointed out that the JSON data could be sent as a form field, and it showed that my reader was missing a bit of data as well. Oddly - another issue was the order of the HTTP params. I can't image why this makes a difference, but if the JSON data isn't the first httpparam, then nothing else works. The final, working code, is below. Note - I removed his API key so you can't run this as is.

<cfset json = '{"method": "create_video", "params": {"video": {"name": "CFHTTP create","shortDescription": "test video"},"token": "falkeapikey"},"filename": "actual2.mp4","maxsize":640000}'>

<cfhttp url="http://api.brightcove.com/services/post" method="post" timeout="600" result="resultVar" multipart="true">

<cfhttpparam type="formfield" name="json" value="#json#"> <cfhttpparam type="file" name="actual2.mp4" file="/Users/ray/Documents/workspace/youtubecfc/unittests/actual2.mp4" /> </cfhttp>

<cfdump var="#resultvar#">

Anyway - I don't have time to dig into the Brightcove API more, but I'm sure someone out there could whip up a quick component.