如何用python发送带有请求的“多部分/表单数据”?

2024-03-28 09:57:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如何用python发送带有请求的multipart/form-data?如何发送一个文件,我理解,但如何通过这种方法发送表单数据却无法理解。


Tags: 文件数据方法form表单datamultipart
3条回答

当您不需要上传任何文件时,您需要使用files参数来发送一个多部分表单POST请求。

来自原始requests源:

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.

相关部分为:file-tuple can be a2-tuple3-tupleor a4-tuple

基于以上,包含要上载的文件和表单字段的最简单的多部分表单请求将如下所示:

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)

请注意,None作为纯文本字段元组中的第一个参数-这是文件名字段的占位符,该字段仅用于文件上载,但对于传递None的文本字段,为了提交数据,需要第一个参数。

多个同名字段

如果需要用相同的名称发布多个字段,则可以将负载定义为元组的列表(或元组),而不是字典:

multipart_form_data = (
    ('file2', ('custom_file_name.zip', open('myfile.zip', 'rb'))),
    ('action', (None, 'store')),
    ('path', (None, '/path1')),
    ('path', (None, '/path2')),
    ('path', (None, '/path3')),
)

流式处理请求API

如果上面的API对您来说不够pythonic,那么考虑使用requests toolbeltpip install requests_toolbelt),它是core requests模块的扩展,提供对文件上传流的支持,还可以使用MultipartEncoder,而不是files,它还允许您将负载定义为字典、元组或列表。

MultipartEncoder可用于包含或不包含实际上载字段的多部分请求。它必须分配给data参数。

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})

如果需要发送多个同名字段,或者如果表单字段的顺序很重要,则可以使用元组或列表而不是字典:

multipart_data = MultipartEncoder(
    fields=(
            ('action', 'ingest'), 
            ('item', 'spam'),
            ('item', 'sausage'),
            ('item', 'eggs'),
           )
    )

自从以前的答案写好后,请求就变了。查看bug thread at Github以获取更多详细信息,并查看this comment以获取示例。

简而言之,files参数采用dict,键是表单字段的名称,值是字符串或2、3或4长度的元组,如请求快速启动中的POST a Multipart-Encoded File部分所述:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': ('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', {'Expires': '0'})}

在上面,元组的组成如下:

(filename, data, content_type, headers)

如果值只是一个字符串,则文件名将与键相同,如下所示:

>>> files = {'obvius_session_id': '72c2b6f406cdabd578c5fd7598557c52'}

Content-Disposition: form-data; name="obvius_session_id"; filename="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

如果值是元组,并且第一个条目是None,则不包括filename属性:

>>> files = {'obvius_session_id': (None, '72c2b6f406cdabd578c5fd7598557c52')}

Content-Disposition: form-data; name="obvius_session_id"
Content-Type: application/octet-stream

72c2b6f406cdabd578c5fd7598557c52

基本上,如果指定一个files参数(字典),那么requests将发送一个multipart/form-data帖子,而不是application/x-www-form-urlencoded帖子。但是,您不限于使用该词典中的实际文件:

>>> import requests
>>> response = requests.post('http://httpbin.org/post', files=dict(foo='bar'))
>>> response.status_code
200

而httpbin.org让你知道你发布了什么标题;在response.json()中我们有:

>>> from pprint import pprint
>>> pprint(response.json()['headers'])
{'Accept': '*/*',
 'Accept-Encoding': 'gzip, deflate',
 'Connection': 'close',
 'Content-Length': '141',
 'Content-Type': 'multipart/form-data; '
                 'boundary=c7cbfdd911b4e720f1dd8f479c50bc7f',
 'Host': 'httpbin.org',
 'User-Agent': 'python-requests/2.21.0'}

更好的是,您可以使用元组而不是单个字符串或字节对象来进一步控制每个部分的文件名、内容类型和附加头。元组应包含2到4个元素;文件名、内容(可选内容类型)和包含更多标题的可选字典。

我将使用以None为文件名的元组形式,以便从这些部分的请求中删除filename="..."参数:

>>> files = {'foo': 'bar'}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--bb3f05a247b43eede27a124ef8b968c5
Content-Disposition: form-data; name="foo"; filename="foo"

bar
--bb3f05a247b43eede27a124ef8b968c5--
>>> files = {'foo': (None, 'bar')}
>>> print(requests.Request('POST', 'http://httpbin.org/post', files=files).prepare().body.decode('utf8'))
--d5ca8c90a869c5ae31f70fa3ddb23c76
Content-Disposition: form-data; name="foo"

bar
--d5ca8c90a869c5ae31f70fa3ddb23c76--

files也可以是两个值元组的列表,如果您需要排序和/或具有相同名称的多个字段:

requests.post(
    'http://requestb.in/xucj9exu',
    files=(
        ('foo', (None, 'bar')),
        ('foo', (None, 'baz')),
        ('spam', (None, 'eggs')),
    )
)

如果同时指定filesdata,则它取决于data将用于创建POST主体。如果data是一个字符串,则只使用它;否则将同时使用datafiles,首先列出data中的元素。

还有一个优秀的requests-toolbelt项目,其中包括advanced Multipart support。它接受与files参数格式相同的字段定义,但与requests不同的是,它默认不设置文件名参数。此外,它还可以流式处理来自open file对象的请求,其中requests将首先在内存中构造请求体:

from requests_toolbelt.multipart.encoder import MultipartEncoder

mp_encoder = MultipartEncoder(
    fields={
        'foo': 'bar',
        # plain file object, no filename or mime type produces a
        # Content-Disposition header with just the part name
        'spam': ('spam.txt', open('spam.txt', 'rb'), 'text/plain'),
    }
)
r = requests.post(
    'http://httpbin.org/post',
    data=mp_encoder,  # The MultipartEncoder is posted as data, don't use files=...!
    # The MultipartEncoder provides the content-type header with the boundary:
    headers={'Content-Type': mp_encoder.content_type}
)

字段遵循相同的约定;使用包含2到4个元素的元组来添加文件名、部分mime类型或额外的头。与files参数不同,如果不使用元组,则不会尝试查找默认的filename值。

相关问题 更多 >