requests-toolbelt
1.官方文档地址:requests-toolbelt官方文档
2.环境安装
pip install requests-toolbelt
multipart/form-data传文件
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields={'field0': 'value',
'field1': 'value',
'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
)
r = requests.post('http://httpbin.org/post',
data=m,
headers={'Content-Type': m.content_type})
multipart/form-data表单提交(传非文件)
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields={'field0': 'value',
'field1': 'value'})
r = requests.post('http://httpbin.org/post',
data=m,
headers={'Content-Type': m.content_type})
多个文件参数重复
1.参数重复的时候,就不用字典了,用list类型
from requests_toolbelt import MultipartEncoder
import requests
m = MultipartEncoder(
fields = [
('source', ('f1.ext', f1, 'application/x-example-mimetype'),
('source', ('f2.ext', f2, 'application/x-example-mimetype'),
]
)
r = requests.post('http://httpbin.org/post',
data=m,
headers={'Content-Type': m.content_type})