A Python Wrapper for Shutterfly. Uploading an Image

Posted by iJames on Stack Overflow See other posts from Stack Overflow or by iJames
Published on 2010-05-07T00:50:27Z Indexed on 2010/05/07 0:58 UTC
Read the original article Hit count: 340

I'm working on a Django app in which I want to order prints through Shutterfly's Open API:

http://www.shutterfly.com/documentation/start.sfly

So far I've been able to build the appropriate POSTs and GETs using the modules and suggested code snippets including httplib, httplib2, urllib, urllib2, mimetype, etc.

But I'm stuck on the image uploading when placing an order (the ordering process is not the same process as uploading images to albums which I haven't tried.)

From what I can tell, I'm supposed to basically create the multipart form data by concatenating the HTTP request body together with the binary data of the image.

I take the strings:

--myuniqueboundary1273149960.175.1
Content-Disposition: form-data; name="AuthenticationID"

auniqueauthenticationid
--myuniqueboundary1273149960.175.1
Content-Disposition: file; name="Image.Data"; filename="1_41_orig.jpg"
Content-Type: image/jpeg

and I put this data into it and end with the final boundary:

...\xb5|\xf88\x1dj\t@\xd9\'\x1f\xc6j\x88{\x8a\xc0\x18\x8eGaJG\x03\xe9J-\xd8\x96[\x91T\xc3\x0eTu\xf4\xaa\xa5Ty\x80\x01\x8c\x9f\xe9Z\xad\x8cg\xba# g\x18\xe2\xaa:\x829\x02\xb4["\x17Q\xe7\x801\xea?\xad7j\xfd\xa2\xdf\x81\xd2\x84D\xb6)\xa8\xcb\xc8O\\\x9a\xaf(\x1cqM\x98\x8d*\xb8\'h\xc8+\x8e:u\xaa\xf3*\x9b\x95\x05F8\xedN%\xcb\xe1B2\xa9~Tw\xedF\xc4\xfe\xe8\xfc\xa9\x983\xff\xd9...

That ends up making it look like this (when I use print to debug):

...
--myuniqueboundary1273149960.175.1
Content-Disposition: file; name="Image.Data"; filename="1_41_orig.jpg"
Content-Type: image/jpeg

????q?ExifMM*
            ?
??(1?2?<??i?b?NIKON CORPORATIONNIKON D40HHQuickTime 7.62009:02:17 13:05:25Mac OS X 10.5.6%??????"?'??0220??????
????    ?
?|_???,b???50??5
...
--myuniqueboundary1273149960.175.1--

My code for grabbing the binary data is pretty much this:

filedata = open('myjpegfile.jpeg','rb').read()

Which I then add to the rest of the body. I've see something like this code everywhere.

I'm then using this to post the full request (with the headers too):

response = urllib2.urlopen(request).read()

This seems to me to be the standard way that form POSTS with files happens.

Am I missing something here?

At some point I might be able to make this into a library worth posting up on github, but this problem has stopped me cold in my tracks.

Thanks for any insight!

© Stack Overflow or respective owner

Related posts about python

Related posts about shutterfly