So, I’ve been slaving away at a web application that connects to its data store via RESTful web services. Because I have to use HTTP PUT and DELETE methods in addition to the usual GET and POST methods, I chose to use Python’s standard library module httplib (urllib and urllib2 only support GET and POST). Everything’s going along great until I get around to a couple of methods I’d been putting off that involve file uploading.
Now, I’m reasonably knowledgeable about HTTP, not an expert by any means, and I’m generally happy to stay out of the gory details as much as possible by using standard library modules. So I was a bit surprised–and dismayed–to discover that if I wanted to submit a multipart form (truly multipart, having both string params and a file) I had to construct the request body by hand.
Of course, constructing a multipart form isn’t terribly difficult, but that’s not the point. Constructing HTTP request headers isn’t difficult, but how many people do it by hand? A file upload form isn’t exactly an edge case, so it’s curious to me that it would not be handled by httplib at this late date (there is a feature request for Python 2.7).
In any case, I’ve decided to use PycURL for the multipart forms. I would be perfectly happy with that if only I didn’t have to translate C API docs into Python (the PycURL docs give you just enough hints that you can get by with some trial and error).