首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用urllib3 UnicodeDecodeError上传文件

使用urllib3 UnicodeDecodeError上传文件
EN

Stack Overflow用户
提问于 2020-03-06 01:29:13
回答 1查看 771关注 0票数 1

我正在尝试使用urllib3通过多部分表单POST请求上传文件。我遵循了urllib docs中的这个示例

代码语言:javascript
复制
>>> with open('example.txt') as fp:
...     file_data = fp.read()
>>> r = http.request(
...     'POST',
...     'http://httpbin.org/post',
...     fields={
...         'filefield': ('example.txt', file_data),
...     })
>>> json.loads(r.data.decode('utf-8'))['files']
{'filefield': '...'}

在修改示例代码时,我添加了一些上传到的API所需的额外字段:

代码语言:javascript
复制
import urllib3

http = urllib3.PoolManager()

with open('/Volumes/GoogleDrive/My Drive/Code/Fuse-Qu/qu/uploads/risk.pdf') as fp:
    file_data = fp.read()

r = http.request(
    'POST',
    'https://***.fuseuniversal.com/api/v4.2/contents/media?auth_token=***',
    fields={
        "name": "test api upload 11",
        "description": "this is a test of uploading a pdf via the api",
        "composite_attributes[type]": "File",
        "community_ids": "24827",
        "composite_attributes[file]": ('risk.pdf', file_data, 'document/pdf'),
    })

然而,我最终得到了这个错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "test-urllib.py", line 6, in <module>
    file_data = fp.read()
  File "/Users/dunc/.pyenv/versions/3.8.1/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe2 in position 10: invalid continuation byte
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-06 01:33:16

您需要以二进制模式打开该文件,因为它不是文本。如果打开文件时未指定binary,python3会自动尝试将内容解码为utf-8。以下是更新后的失败行:

代码语言:javascript
复制
with open('/Volumes/GoogleDrive/My Drive/Code/Fuse-Qu/qu/uploads/risk.pdf', 'rb') as fp:
    file_data = fp.read()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60550990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档