在 Python 请求中使用 POST 表单数据上传图像 [英] Upload Image using POST form data in Python-requests

查看:35
本文介绍了在 Python 请求中使用 POST 表单数据上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用微信 API ...在这里,我必须使用此 API 将图像上传到微信服务器http://admin.wechat.com/wiki/index.php?title=传输_多媒体_文件

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

url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=%s&type=image'%access_token
files = {
    'file': (filename, open(filepath, 'rb')),
    'Content-Type': 'image/jpeg',
    'Content-Length': l
}
r = requests.post(url, files=files)

我无法发布数据

推荐答案

来自微信api文档:

curl -F media=@test.jpg "http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"

将上面的命令翻译成python:

Translate the command above to python:

import requests
url = 'http://file.api.wechat.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE'
files = {'media': open('test.jpg', 'rb')}
requests.post(url, files=files)

文档:https://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file

这篇关于在 Python 请求中使用 POST 表单数据上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆