是否有 Python HTTP 客户端允许您在 Content-Disposition 中设置大小以发布文件?

Is there a Python HTTP client that lets you set the size in Content-Disposition for posting files?

我正在将文件从 Python 发布到供应商的 API,而供应商的 API 抱怨 Content-Disposition [=26] 中的内容缺少大小=].他们给出的例子是这样的:

Content-Disposition: form-data;文件名=文件名;名字=名字;尺寸=1234

是否有一个 Python HTTP 客户端可以让我从头开始包含没有 re-writing 的大小的所有内容? Requests 使用 urllib3 进行文件 POSTing,而且那些似乎不支持设置文件附件的大小。

郑重声明,以下是我最终解决问题的方式:

from requests.packages.urllib3.fields import RequestField
from requests.packages.urllib3.filepost import encode_multipart_formdata


def prepare_body_with_size(request, files):
    new_fields = []
    for name, filename, data, file_type in files:
        rf = RequestField(name=name, data=data, filename=filename)
        content_disposition = 'form-data; size=%d' % len(data)
        rf.make_multipart(content_disposition=content_disposition, content_type=file_type)
        new_fields.append(rf)

    body, content_type = encode_multipart_formdata(new_fields)
    request.headers['Content-Type'] = content_type
    request.body = body
    return request

from requests import Request, Session

with Session() as s:
    req = Request('POST', POST_ENDPOINT)
    prepped = req.prepare()
    prepare_body_with_size(prepped, files)
    response = s.send(prepped)

大部分代码只是重写了请求的 prepare_body 方法的工作方式。