<p>当您不需要上传任何文件时,您需要使用<code>files</code>参数来发送一个多部分表单POST请求。</p>
<p>来自原始<a href="https://github.com/kennethreitz/requests/blob/master/requests/api.py#L16" rel="nofollow noreferrer">requests</a>源:</p>
<pre class="lang-python prettyprint-override"><code>def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request <Request>`.
...
:param files: (optional) Dictionary of ``'name': file-like-objects``
(or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``,
3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``,
where ``'content-type'`` is a string
defining the content type of the given file
and ``custom_headers`` a dict-like object
containing additional headers to add for the file.
</code></pre>
<p>相关部分为:<em><code>file-tuple can be a</code><strong/></em><code>2-tuple</code>,<strong><code>3-tuple</code></strong><code>or a</code><strong><code>4-tuple</code></strong>。</p>
<p>基于以上,包含要上载的文件和表单字段的最简单的多部分表单请求将如下所示:</p>
<pre class="lang-python prettyprint-override"><code>multipart_form_data = {
'file2': ('custom_file_name.zip', open('myfile.zip', 'rb')),
'action': (None, 'store'),
'path': (None, '/path1')
}
response = requests.post('https://httpbin.org/post', files=multipart_form_data)
print(response.content)
</code></pre>
<p>☝ <em>请注意,<code>None</code>作为纯文本字段元组中的第一个参数-这是文件名字段的占位符,该字段仅用于文件上载,但对于传递<code>None</code>的文本字段,为了提交数据,需要第一个参数。</em></p>
<h3>多个同名字段</h3>
<p>如果需要用相同的名称发布多个字段,则可以将负载定义为元组的列表(或元组),而不是字典:</p>
<pre><code>multipart_form_data = (
('file2', ('custom_file_name.zip', open('myfile.zip', 'rb'))),
('action', (None, 'store')),
('path', (None, '/path1')),
('path', (None, '/path2')),
('path', (None, '/path3')),
)
</code></pre>
<hr/>
<h3>流式处理请求API</h3>
<p>如果上面的API对您来说不够pythonic,那么考虑使用<a href="http://toolbelt.readthedocs.org/en/latest/uploading-data.html#streaming-multipart-data-encoder" rel="nofollow noreferrer">requests toolbelt</a>(<code>pip install requests_toolbelt</code>),它是<a href="http://docs.python-requests.org/en/master/" rel="nofollow noreferrer">core requests</a>模块的扩展,提供对文件上传流的支持,还可以使用<a href="http://toolbelt.readthedocs.io/en/latest/user.html#multipart-form-data-encoder" rel="nofollow noreferrer">MultipartEncoder</a>,而不是<code>files</code>,它还允许您将负载定义为字典、元组或列表。</p>
<p><code>MultipartEncoder</code>可用于包含或不包含实际上载字段的多部分请求。它必须分配给<code>data</code>参数。</p>
<pre class="lang-python prettyprint-override"><code>import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
multipart_data = MultipartEncoder(
fields={
# a file upload field
'file': ('file.zip', open('file.zip', 'rb'), 'text/plain')
# plain text fields
'field0': 'value0',
'field1': 'value1',
}
)
response = requests.post('http://httpbin.org/post', data=multipart_data,
headers={'Content-Type': multipart_data.content_type})
</code></pre>
<p>如果需要发送多个同名字段,或者如果表单字段的顺序很重要,则可以使用元组或列表而不是字典:</p>
<pre><code>multipart_data = MultipartEncoder(
fields=(
('action', 'ingest'),
('item', 'spam'),
('item', 'sausage'),
('item', 'eggs'),
)
)
</code></pre>