Error with put_photo - UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 413: ordinal not in range(128) #33

sontek opened this Issue May 21, 2012 · 12 comments

3 participants

(Pdb) image
<open file '/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/photos/uploads/upload_11.jpg', mode 'r' at 0x147f4b0>
(Pdb) c

Traceback (most recent call last):
File "/home/sontek/.virtualenvs/studiosnaps/local/lib/python2.7/site-packages/gevent/", line 390, in run
result = self._run(self.args, *self.kwargs)
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/", line 114, in upload_photo
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/", line 283, in upload_photo
photo_response = fp_graph.put_photo(photo, '', album_id)
File "/home/sontek/.virtualenvs/studiosnaps/src/studiosnaps/booth/", line 175, in put_photo
object_id = album_id or "me"
File "/usr/lib/python2.7/", line 126, in urlopen
return, data, timeout)
File "/usr/lib/python2.7/", line 400, in open
response = self._open(req, data)
File "/usr/lib/python2.7/", line 418, in _open
'_open', req)
File "/usr/lib/python2.7/", line 378, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/", line 1215, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/usr/lib/python2.7/", line 1174, in do_open
h.request(req.get_method(), req.get_selector(),, headers)
File "/usr/lib/python2.7/", line 958, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/", line 992, in _send_request
File "/usr/lib/python2.7/", line 954, in endheaders
File "/usr/lib/python2.7/", line 812, in _send_output
msg += message_body
UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 413: ordinal not in range(128)

I'm just calling:

            photo_response = graph_obj.put_photo(photo, '', album_id)
@sontek at line 190 has this fnky this_is_the_boundary stuff in it:

\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x01E\x14P\x07\xff\xd9\r\n------------ThIs_Is_tHe_bouNdaRY_$\r\nContent-Disposition: form-data; name="message"\r\n\r\nhi\r\n------------ThIs_Is_tHe_bouNdaRY_$--\r\n'


Do you still get this error if you open the file in binary mode? If so, would it be possible for you to provide me with the image that is causing the error?


According to the docs binary mode only matters on windows and I'm on Linux. It also says it could corrupt JPEG files. I did try it but got the same exception.

Uploading the image does work with the facepy library that you can find here on github to compare how they are doing multipart forms.

Here is the failing photo:


If it helps, this was uploading a photo to a page's album, not a personal album.


I tried to replicate this error using the image file you provided, but I have been unable to (the image uploaded successfully each time). Can you confirm that you are using the most recent version of this library?


Yes, I was using the current version from master.

I'm using python 2.7.3 on Ubuntu LTS 12.04.

I was uploading to a fan/company page, not a personal profile page.


One of the computers I tested on is running python 2.7.3rc2. Since the error you are receiving is a UnicodeDecodeError, it has nothing to do with the type of Facebook profile you are trying to upload the photo to.

I tried more testing, but can still not replicate the error you are receiving. I noticed that your original traceback mentions gevent. Do you still get the error if you are not using it (i.e. using python on the command line)?


@sontek, I have not heard anything from you on this, and there have been no additional reports of people with the same issue, so I am going to close this.

@martey martey closed this Jul 16, 2012

I'm getting a UnicodeDecodeError while trying to upload a PIL generated PNG, stored in a django model.

If I try to upload another file, it works fine. So I think it must be related with how I'm generating the image or something with PIL. I know I'm not providing much information, but do you have any ideas?


Digging a bit more, I see that put_photo assumed the image is JPG:

Why is that? Facebook doesn't allow PNG?


Why is that? Facebook doesn't allow PNG?

Some documentation from the old REST API I found suggests that the following file types are supported:


This is a bug in the library, but should be created as a new issue (since it is not the same issue as was originally reported).


Created 2 issues. Posting here for reference:
UnicodeDecodeError: #43
Hardcoded JPG format: #44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment