根据 [docs][1,您需要向元组添加两个参数:文件名和内容类型:
# field name filename file object content=type
files = {'location[logo]': ("name.png", open(fileinput),'image/png')}
您可以在下面查看示例:
In [1]: import requests
In [2]: files = {'location[logo]': ("foo.png", open("/home/foo.png"),'image/png')}
In [3]:
In [3]: ses = requests.session()
In [4]: res = ses.put("http://httpbin.org/put",files=files)
In [5]: print(res.request.body[:200])
--0b8309abf91e45cb8df49e15208b8bbc
Content-Disposition: form-data; name="location[logo]"; filename="foo.png"
Content-Type: image/png
�PNG
IHDR��:d�tEXtSoftw
备查,这条评论 https://github.com/kennethreitz/requests/issues/1495在一个旧的相关问题中解释了所有变化:
# 1-tuple (not a tuple at all)
{fieldname: file_object}
# 2-tuple
{fieldname: (filename, file_object)}
# 3-tuple
{fieldname: (filename, file_object, content_type)}
# 4-tuple
{fieldname: (filename, file_object, content_type, headers)}