当前位置: 首页 > 后端技术 > Python

python发送form-data数据

时间:2023-03-26 12:25:48 Python

最近在连接其他接口的时候,想发送form-data数据,想着在headers中给multipart/form-data加上Content-Type,可能是我太天真了,之后添加,所有传入的数据都将丢失,这是非常痛苦的。于是到处查找资料,最终为有需要的同志找到了以下两种解决方案。requests_toolbeltfromrequests_toolbeltimportMultipartEncoderimportrequestsm=MultipartEncoder(fields={'field0':'value','field1':'value','field2':('文件名',open('文件地址/file.py','rb'),'text/plain')})r=requests.post('http://httpbin.org/post',data=m,headers={'Content-Type':m.content_type})urlib3encode_multipart_formdatafromurlib3importencode_multipart_formdataimportrequestsm=encode_multipart_formdata()r=requests.post('http://httpbin.org/post',data=m[0],headers={'Content-Type':m[1]})