Python中的多部分POST请求没有文件名

问题描述 投票:0回答:1

我期待从python API发送以下类型的POST请求:

------WebKitFormBoundarykQ0pCUTfUvTIocvw
Content-Disposition: form-data; name="filename"

SSF_nY4PQT8190207X10Showtest.gz
------WebKitFormBoundarykQ0pCUTfUvTIocvw--

请注意,SSF_nY4PQT8190207X10test.gz不是我发送的文件。我要求服务器对此文件执行某些操作。

以下链接我尝试通过t = s.post(url, headers=headers, files = {'filename': ('', 'content')}, verify=False)执行此操作

Multipart POST using python requests

https://github.com/requests/requests/issues/935

但得到这个头

--63fb60c0045e41439442713330f821ce
Content-Disposition: form-data; name="filename"; filename=""

content
--63fb60c0045e41439442713330f821ce--

如何在我的请求中摆脱filename=""

在此先感谢您的帮助

python python-2.7 python-requests
1个回答
0
投票

试图用这个脚本玩一轮:

#!/usr/bin/env python3

from requests import Request, Session

s = Session()
files={'filename': 'content'}
url="http://localhost"
req = Request('POST',  url, files=files)

prepped = s.prepare_request(req)
print(prepped.body)

但无论我做了什么,Content_Disposition总是有Content-Disposition: form-data; name=""; filename=""参数。

所以我查看了请求来源:https://github.com/requests/requests/blob/master/requests/models.py#L165,它将我推荐给Urllib3 https://github.com/urllib3/urllib3/blob/master/src/urllib3/fields.py#L174 即使你提供空字符串,似乎这些参数也会一直存在。没有办法覆盖它们。

编辑 从以上评论之一:

#!/usr/bin/env python3

from requests import Request, Session

s = Session()
files={'filename': (None,'content')}
url="http://localhost"
req = Request('POST',  url, files=files)

prepped = s.prepare_request(req)
print(prepped.body)
© www.soinside.com 2019 - 2024. All rights reserved.