1.'content-type':'application/x-www-form-urlencoded'
data参数提交文本或字典都可以
headers为空时,data提交content-type默认也是application/x-www-form-urlencoded
| requests.post(url,headers={'content-type':'application/x-www-form-urlencoded'},data='f=10') |
| requests.post(url,headers={'content-type':'application/x-www-form-urlencoded'},data={'f':10}) |
2.'content-type':'application/json'
data参数提交或json参数提交
data参数提交:注意str必须是json.dumps()转换的标准的json字符串,而非str(),这两者并不完全等同。
json参数提交:模块会自动将dict转换为json提交。
| requests.post(url,headers={'content-type':'application/json'},data=json.dumps({'f':10})) |
| requests.post(url,headers={'content-type':'application/json'},json={'f':10}) |
3.'content-type':'text/xml'
data参数提交<bytes>
- 通常用于上传xml格式文本等;将文本
<str>.encode("utf-8")
编码为bytes类型上传
| requests.post(url,headers={'content-type':'text/xml'},data='<xml......>'.encode("utf-8")) |
4. 'content-type':'multipart/formdata'
- files参数提交
<dict>
- 用于上传文件;通常Content-Type中除了
Content-Type: multipart/form-data
,还有个boundary=随机字符串
,该项的作用是当作提交内容的分隔符,构造files时用不到可以忽略。
| Request Headers |
| Content-Type: multipart/form-data; boundary= |
| |
| FormData |
| file: (binary) |
| else1: xxx1 |
| else2: xxx2 |
| |
| FormData view source |
| |
| Content-Disposition: form-data; name="file"; filename="test.jpg" |
| Content-Type: image/jpeg |
| |
| |
| |
| Content-Disposition: form-data; name="else1" |
| |
| xxx1 |
| |
| Content-Disposition: form-data; name="else2" |
| |
| xxx2 |
| |
如下方式提交
| files = { |
| "file":("test.jpg", open(r"D:\test\test.jpg",'rb')), |
| "else1": (None,"xxx1"), |
| "else2": (None,"xxx2") |
| } |
| r = requests.post(url,files=files) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)