是否可以在不发送文件的情况下使用 python 请求发送 multipart/form-data?
我的请求头应该是这样的:
--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="value";
content
--3eeaadbfda0441b8be821bbed2962e4d--
但实际上它看起来像这样:
--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: text/plain
content
--3eeaadbfda0441b8be821bbed2962e4d--
我正在使用这个函数:
response = requests.post('url', data, files=('file', 'file'))
最佳答案
是的,你可以,只需将带有元组的字典作为值传递给 post 方法。当您为此方法 requests
指定 files
参数时,添加 multipart/form-data
header 。但是你可以自由地将带有元组的字典作为 files
传递:
url = 'http://httpbin.org/post'
files = {'file': (None, 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files)
http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
关于Python请求,没有文件的多部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451685/