python请求如何处理Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryvAREXEr68BCFPMb0

关键点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
url = "http://xxxxxxx"

fields = {
'file': ('文件名.xlsx', open("文件名.xlsx", "rb"), "xlsx"),
"careId": '2208071500000033',
"selectUserType": "3"
}
# 因为16位数随机的,每次都不一样
boundary = '----WebKitFormBoundary' \
+ ''.join(random.sample(string.ascii_letters + string.digits, 16))
m = MultipartEncoder(fields=fields, boundary=boundary)
headers = {
"Content-Type": m.content_type
}

req = s.post(url=url, headers=headers,
data=m)
print(req.json())
 
posted @ 2022-08-07 18:01  爱好者zz  阅读(3768)  评论(1编辑  收藏  举报