Python Request: Post Images on Facebook using Multipart/form-data -


i'm using facebook api post images on page, can post image web using :

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 want post local image (using multipart/form-data) error : valueerror: data must not string.

i using 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 , file in single request) maybe it's not possible send both data , files in multipart encoded file updated 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 doesn't seem work, same error above.
guys know why it's not working, , maybe way fix this.

pass in data 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 files uploading) application/x-www-form-urlencoded encoded string.

using dictionary post data has additional advantage requests takes care of encoding values; caption contain data must escape properly.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -