2

I'm using the facebook API to post images on a page, I can post image from web using this :

import requests

data = 'url=' + url + '&caption=' + caption + '&access_token=' + token
status = requests.post('https://graph.facebook.com/v2.7/PAGE_ID/photos',
                       data=data)
print status

But when I want to post a local image (using multipart/form-data) i get the error : ValueError: Data must not be a string.

I was using this code:

data = 'caption=' + caption + '&access_token=' + token
files = {
    'file': open(IMG_PATH, 'rb')
    }

status = requests.post('https://graph.facebook.com/v2.7/PAGE_ID/photos',
                       data=data, files=files)
print status

I read (Python Requests: Post JSON and file in single request) that maybe it's not possible to send both data and files in a multipart encoded file so I updated my code :

data = 'caption=' + caption + '&access_token=' + token
files = {
    'data': data,
    'file': open(IMG_PATH, 'rb')
    }

status = requests.post('https://graph.facebook.com/v2.7/PAGE_ID/photos',
                       files=files)
print status

But that doesn't seem to work, I get the same error as above.
Do you guys know why it's not working, and maybe a way to fix this.

Community
  • 1
  • 1
plean
  • 124
  • 1
  • 9

1 Answers1

3

Pass in data as a dictionary:

data = {
    'caption', caption,
    'access_token', token
}
files = {
    'file': open(IMG_PATH, 'rb')
}
status = requests.post(
    'https://graph.facebook.com/v2.7/PAGE_ID/photos',
     data=data, files=files)

requests can't produce multipart/form-data parts (together with the files you are uploading) from a application/x-www-form-urlencoded encoded string.

Using a dictionary for the POST data has the additional advantage that requests takes care of properly encoding the values; caption especially could contain data that you must escape properly.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Which 'name' does the content-disposition header use for sending along such a dictionary of json data? I can't find this anywhere, but would really like to make use of it in my C# program. Currently I'm stuck adding multiple bits of StringContent to my MultipartFormDataContent. – Jay Apr 25 '18 at 14:29
  • Hi, How to upload multiple photo using same method. I tried putting two item in files dict . It stills upload one photo. – iamabhaykmr Mar 20 '19 at 09:59
  • 1
    @ascii_walker: the page photos API doesn't support uploading multiple photos in a single request. See the [documentation](https://developers.facebook.com/docs/graph-api/reference/page/photos/#Creating): *You can upload and publish a single photo in one API request.* – Martijn Pieters Mar 20 '19 at 12:14
  • In using this, can files dict contain multiple images that could then be all posted together in one post on facebook? – Jared Scarito Mar 24 '19 at 15:08
  • No, it can’t. See my previous comment. – Martijn Pieters Mar 24 '19 at 15:32
  • Thanks @MartijnPieters. It saved mine R&D time. – iamabhaykmr Apr 10 '19 at 05:01