Python requests.post multipart/form-data
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33404833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python requests.post multipart/form-data
提问by Amaranth
I have to use a REST API to upload files and information to a server. That API uses a multipart-form, but I cannot seem to be able to use it correctly.
我必须使用 REST API 将文件和信息上传到服务器。该 API 使用多部分形式,但我似乎无法正确使用它。
Here is the information I use according to the API documentation.
这是我根据 API 文档使用的信息。
Form Parameters:
表格参数:
- description – A short description of the distribution.
- release_notes_url – A url pointing to the release notes.
- zip_file – The ZIP file containing the distribution files.
- description – 分布的简短描述。
- release_notes_url – 指向发行说明的 url。
- zip_file – 包含分发文件的 ZIP 文件。
Example request:
示例请求:
POST /api/v1/distribution HTTP/1.1
Host: api.company.onbe
Authorization: t=...
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryZayrf7leHxinyQsX
------WebKitFormBoundaryZayrf7leHxinyQsX
Content-Disposition: form-data; name="release_notes_url"
http://releases/3.0.0/release_notes_3_0_0.pdf
------WebKitFormBoundaryZayrf7leHxinyQsX
Content-Disposition: form-data; name="description"
This is the new distribution!
------WebKitFormBoundaryZayrf7leHxinyQsX
Content-Disposition: form-data; name="zip_file"; filename="BackEnd-3.0.0.zip"
Content-Type: application/x-zip-compressed
------WebKitFormBoundaryZayrf7leHxinyQsX--
I tried several things, like the following code for example, but I keep getting bad request errors from the server.
我尝试了几件事,例如下面的代码,但我不断收到来自服务器的错误请求错误。
import requests
file= open('BackEnd-3.0.0.zip','r').read()
url = 'api.company.onbe/api/v1/distribution'
payload = {
'description' :'Some desc',
'release_notes_url':'Someurl.pdf',
'zip_file': file
}
response = requests.post(url, data=payload)
采纳答案by Josh J
The docs have an example http://requests.readthedocs.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
文档有一个示例http://requests.readthedocs.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
You should really start there for simple use cases.
对于简单的用例,您真的应该从那里开始。
This answer also explains using files and data together.
这个答案还解释了如何一起使用文件和数据。
https://stackoverflow.com/a/12385661/1182891
https://stackoverflow.com/a/12385661/1182891
Here is a working example for people who want cut-n-paste code. httpbin returns a json data structure describing the request you made do it. In this case you can see that files
contains the file data posted and form
contains the form vars. headers
shows that it was indeed a multipart/form-data
request.
这是一个适用于想要剪切粘贴代码的人的工作示例。httpbin 返回一个 json 数据结构,描述您所做的请求。在这种情况下,您可以看到files
包含发布的文件数据并form
包含表单 vars。headers
表明这确实是一个multipart/form-data
请求。
>>> import requests
>>> from pprint import pprint
>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.csv', 'some,data,to,send\nanother,row,to,send\n')}
>>> response = requests.post(url, data={
... 'description' :'Some desc',
... 'release_notes_url':'Someurl.pdf'
... }, files=files)
>>> pprint(response.json())
{u'args': {},
u'data': u'',
u'files': {u'file': u'some,data,to,send\nanother,row,to,send\n'},
u'form': {u'description': u'Some desc', u'release_notes_url': u'Someurl.pdf'},
u'headers': {u'Accept': u'*/*',
u'Accept-Encoding': u'gzip, deflate',
u'Content-Length': u'394',
u'Content-Type': u'multipart/form-data; boundary=ebf9f03029db4c2799ae16b5428b06bd',
u'Host': u'httpbin.org',
u'User-Agent': u'python-requests/2.10.0'},
u'json': None,
u'origin': u'73.0.41.38',
u'url': u'http://httpbin.org/post'}
Enjoy!
享受!