Python 请求上传文件接口
def post_multipart_request():
params = {"key1": "value1", "key2": "value2"}
files = {
'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream')
}
r = requests.post(url, files=files, params=params)
print(r.content)
Python 上传多个文件
files = {
"api_field1" : ("file_name", open("file_path", "rb")),
"api_field2" : ("file_name", open("file_path", "rb"), "image/jpeg"),
"api_field3" : ("file_name", open("file_path", "rb"), "image/jpeg", {"header-param" : "xxx"})
}
Python如何将JSON作为multipart POST-request的一部分
json_content = {"key1": "value1", "key2": "value2"}
files = {
'json': (None, json.dumps(json_content, ensure_ascii=False), 'application/json'),
'file': (os.path.basename(file), open(file, 'rb'), 'application/octet-stream')
}