【问题标题】:How to make a post request that includes open files? [duplicate]如何发出包含打开文件的发布请求? [复制]
【发布时间】:2019-04-20 04:56:19
【问题描述】:

Мy 查询产生以下错误

{"ExportedDataFile":["ExportedDataFile 字段为必填项。"]}

import requests

files = {'file': open('33332815-2019-04-191419.xml', 'rb')}
payload = {'ExportedDataFile': files, 'Token': 'test'}
r = requests.post("http://????", data=payload)
print(r.text)

【问题讨论】:

  • 为什么在data 映射中将files 字典作为ExportedDataFile 键传递?您需要将该字典作为files 参数传递,可能将该字典中的键设置为ExportedDataFile,因此files = {'ExportedDataFile': open(...)}payload = {'Token': 'test'},然后requests.post(url, data=payload, files=files)。查看副本。
  • 您在{'file': ...} 中传递了一个打开的文件对象。文件应该通过files 参数传递给requests.post,假设另一端的服务器期待一些多部分的MIME 文档:docs.python-requests.org/en/master/api/#requests.request

标签: python http request


【解决方案1】:

正如@Martijn Pieters 指出的那样,您可以像这样发布文件:

import requests

files = {'ExportedDataFile': open('33332815-2019-04-191419.xml', 'rb')}
payload = {'Token': 'test'}
r = requests.post("http://????", files=files, data=payload)
print(r.text)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-24
  • 1970-01-01
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 1970-01-01
  • 2011-04-11
相关资源
最近更新 更多