一、请求头不加content-type
header = {"token": "123456"}
data = {"tag": "tag1"}
url = "..../uploadfile"
file_path = "文件路径"
file_name = os.path.basename(file_path)
file = {
"file": open(image_path, "rb"),
"Content-Type": "image/png", # 根据实际
"Content-Disposition": "form-data",
"filename": file_name
}
req = requests.request(url=url,method="post",header=header,data=json.loads(data),files=file)
二、请求头加content-type
from requests_toolbelt import MultipartEncoder
header = {"token": "123456"}
url = "..../uploadfile"
file_path = "文件路径"
file_name = os.path.basename(file_path)
m = MultipartEncoder(
fields={
"tag": "tag1",
"filename": file_name,
"file":(file_name, open(file_path, 'rb'), 'image/png') # 根据实际
}
)
header.update({"Content-Type": m.content_type})
req = requests.request(url=url,method="post",header=header,data=m)