I’m working with wechat APIs … here I’ve to upload an image to wechat’s server using this API http://admin.wechat.com/wiki/index.php?title=Transferring_Multimedia_Files
JavaScript
x
8
1
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
2
files = {
3
'file': (filename, open(filepath, 'rb')),
4
'Content-Type': 'image/jpeg',
5
'Content-Length': l
6
}
7
r = requests.post(url, files=files)
8
I’m not able to post data
Advertisement
Answer
From wechat api doc:
JavaScript
1
2
1
curl -F media=@test.jpg "http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"
2
Translate the command above to python:
JavaScript
1
5
1
import requests
2
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE'
3
files = {'media': open('test.jpg', 'rb')}
4
requests.post(url, files=files)
5
Doc: https://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file