关键点post时headers中boundary和data中的boundary数据要一致
'Content-Type': f'multipart/form-data; boundary=----{web_boundary}',
随机生成16位大小写字母+数字
import requests
import random,string
from requests_toolbelt import MultipartEncoder
fields = {
'file': ('test.png', your_data, "image/png"),
'file_id': "0"
}
boundary = '----WebKitFormBoundary' \
+ ''.join(random.sample(string.ascii_letters + string.digits, 16))
m = MultipartEncoder(fields=fields, boundary=boundary)
headers = {
"Host": "xxxx",
"Connection": "keep-alive",
"Content-Type": m.content_type
}
req = requests.post('https://xxxx/api/upload', headers=headers, data=m)
print(req.text)