I am using a python library (poster) that takes a file-like object as an argument, the documentation states:
The file-like objects must support .read() and either .fileno() or both .seek() and .tell().
I have tried the library using the python open
function and it works fine. I am currently downloading an image from a URL using the following:
access_token = "XXXXXXXXXXXXXXXXXXXXX"
postPhotoUrl = "https://graph.facebook.com/me/photos?access_token=%s" % access_token
register_openers()
# get image from external URL
data = urllib2.urlopen("http://example.com/image.png")
data = StringIO(data.read())
### data, headers = multipart_encode({"source":open("file.png")}) <- WORKS FINE
data, headers = multipart_encode({"source":data})
request = urllib2.Request(postPhotoUrl,data,headers)
EDIT: My goal is to fetch an image from an external URL and POST it using the facebook graph api. When I use the python open
function I have no issues. When I try to use StringIO, no body is sent with the POST request.