About four weeks ago, Constant Contact released an update to our API offering. We added access to our Image Hosting capabilities through our RESTful API platform, just in time to bring more value to our upgraded Document Hosting features. Of course, I had to play with these new APIs as soon as they came out (partially to support our Developer Challenge and anyone using them there).
A little background on where I’m coming from, I’m an old school C/C++ programmer who’s dabbled in everything from Lisp and Perl to ActionScript and .NET. My platform of choice these days is C# .NET. I’m really enjoying cloud programming through the Azure platform and mixing the powerful application features with the easy to use web development tools in Visual Studio 2010 and the CLR 4.0. All the development work I did for our APIs was done using C#.NET (min version 2.5) and the sample code is available through SourceForge. Disclaimer: best practices were not always followed in designing this code.
Starting off, I read through the documentation on our website to learn more about how to create this multipart/form-data request. The best piece of information we offered for developing the code was the RFC detailing how to make the request. Since I know how much everyone loves reading RFCs, here’s the nutshell version – the body is separated into multiple parts using the following format:
--Boundary **blank line** Image XML Headers Image XML --Boundary ** blank line** Image Data Headers Image as binary --Boundary--
So this may look simple, but the actual request body turns out to be very fragile in practice. One incorrect line return and you’ll get some pretty tough to troubleshoot error messaging. The basics of setting up your boundaries are that they must be a string that is unique in the message, i.e. it won’t occur randomly in your XML or binary image data. When putting your boundaries into the body of the email, you must prefix them with –– and the final boundary must also be postfixed with an additional ––. That last piece is very important and subtle in the RFC documentation, big shout out to our Web Services Developer staff who worked with me troubleshooting that error.
In the end, writing my own code for processing these requests turned out to be pretty simple. I was able to reuse most of the same code from other requests and our existing .NET libraries and build on our already existing code base. While I couldn’t find a library in my 2-3m of searching the web to do multipart/form-data requests in .NET, this didn’t cost me much in time. If anyone knows of a library for this or has any questions about our APIs or my code, feel free to send me an email through our AppConnect program.
Don’t forget, if you think of any ideas for how to use this or any of our other APIs, you could win up to $15,000 in our Developer Challenge. Simply code an application, mobile app or plugin to existing software, submit and sit back while we judge!
* Please be aware that all comments are moderated.
Redes Sociales | 5:30 PM January 31, 2011
im gonna try that api out, thanks