本文主要是介绍Python实现requests-post(Multipart/form-data格式)boundary=----WebKitForm,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这种模式相比于普通post,实在太烦了,这种基本都是用来上传文件(包括图片、excel、doc等)
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import jsonurl = 'http://www.requests-post.com'
headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7','Accept-Encoding': 'gzip, deflate','Accept-Language': 'zh-CN,zh;q=0.9','Cache-Control': 'max-age=0','Connection': 'keep-alive','Content-Length': '19520','Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryH8mH2ATzEdwAUiAF','Host': 'www.requests-post.com:38075','Origin': 'http://www.requests-post.com:38075','Referer': 'http://www.requests-post.com:38075/','Upgrade-Insecure-Requests': '1','User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
}
multipart_encoder = MultipartEncoder(fields={"jsonStr":json.dumps({"valMap": {"excelCode": "wkWorkDic_export", "username": "管理员", "contrast_type": "","error_handle_type": "0"}, "excel_config_code": "wkWorkDic_export","excel_config_sheet_name": "Sheet1"}),'firstfile': ('mb.xls', open('mb.xls', 'rb'), 'application/octet-stream')},
boundary = '----WebKitFormBoundarymxkDwz8XGpb83hah' )headers['Content-Type'] = multipart_encoder.content_type
# 请求头必须包含一个特殊的头信息,类似于Content-Type: multipart/form-data; boundary=${bound}r = requests.post(url, data=multipart_encoder, headers=headers)
print(r.text)
这篇关于Python实现requests-post(Multipart/form-data格式)boundary=----WebKitForm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!