1. MultipartEncoder: 用来搭配Content-Type: multipart/form-data完成文件的上传。用于post请求
import requests from requests_toolbelt import MultipartEncoder #方式一 encoder = MultipartEncoder({'field': 'value', 'other_field', 'other_value'})
"""field
:服务端约定的上传文件字段名。一般用到的是file
,需要和服务端沟通获取。""" r = requests.post('https://httpbin.org/post', data=encoder, headers={'Content-Type': encoder.content_type})
#方式二
encoder = MultipartEncoder({
'field': ('file_name', b'{"a": "b"}', 'application/json',
{'X-My-Header': 'my-value'})
])
python使用requests提交post请求并上传文件(multipart/form-data
2.
标签:框架,encoder,field,pytest,MultipartEncoder,requests,post,data,第三方 From: https://www.cnblogs.com/lintest/p/17163239.html