python - 使用 urllib3 UnicodeDecodeError 上传文件
问题描述
我正在尝试使用 urllib3 通过多部分表单 POST 请求上传文件。我从urllib 文档中遵循了这个例子:
>>> 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 所需的额外字段:
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'),
})
但是我最终得到了这个错误:
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
解决方案
您需要以二进制模式打开文件,因为它不是文本。如果您在未指定二进制文件的情况下打开文件,python3 会自动尝试将内容解码为 utf-8。这是更新的失败行:
with open('/Volumes/GoogleDrive/My Drive/Code/Fuse-Qu/qu/uploads/risk.pdf', 'rb') as fp:
file_data = fp.read()
推荐阅读
- javascript - GoJS 调整图表每个元素的大小
- html - 两个相同的html元素有不同的尺寸
- python - 查找 Xpath 对每个登录都是唯一的元素
- python - 如何在pdf中提取输入字段的x0,y0坐标
- flutter - 在 DataTable 中显示所选单元格的详细数据
- java - 带有@Param null 参数的 JPA 仍然抛出错误
- javascript - 如何重新启动已设置间隔的倒数计时器?Javascript
- azure - 如何将 Ubuntu VM 从 Digital Ocean 迁移到 Azure
- reactjs - 材质 UI 工具提示未在嵌套组件上呈现
- asciidoc - asciidoc:源块对齐到嵌套列表项