Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用 python requests 上传文件名包含中文问题 #1

Closed
wyttg opened this issue Mar 16, 2018 · 1 comment
Closed

使用 python requests 上传文件名包含中文问题 #1

wyttg opened this issue Mar 16, 2018 · 1 comment

Comments

@wyttg
Copy link

wyttg commented Mar 16, 2018

使用 requests 可以成功上传英文文件名的文件,但不能上传文件名包含中文的情况。
能否给出使用 requests 上传文件名包含中文的示例?

我目前的做法:
f = open(u'测试.txt', 'rb')
url = '''https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=file''' % ac_token
files = {'file': ('测试.txt', f)}
r = requests.post(url, files=files)

返回的是
{u'errcode': 44001, u'errmsg': u'empty media data, hint: [ ], more info at https://open.work.weixin.qq.com/devtool/query?e=44001'}

看起来是 requests 将 Content-Disposition 部分写为了:
Content-Disposition: form-data; name="file"; filename*=utf-8''%E6%B5%8B%E8%AF%95.txt

使用 API 测试页面 可以成功上传中文文件名的文件,看到的 Request Payload 为:
Content-Disposition: form-data; name="media"; filename="测试.txt"

@sbzhu
Copy link
Owner

sbzhu commented May 23, 2018

python 要使用中文,需要在 文件开头加 #coding=utf-8
具体可百度

@sbzhu sbzhu closed this as completed May 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants