前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python Requests Post for 5 Styles

Python Requests Post for 5 Styles

作者头像
happy123.me
发布2019-12-30 18:18:03
5740
发布2019-12-30 18:18:03
举报
文章被收录于专栏:乐享123乐享123
requests库发送post请求的五种姿势;
1.application/x-www-form-urlencoded

最常见的 POST 提交数据的方式了。浏览器的原生 form 表单,如果不设置 enctype属性,那么最终就会以 application/x-www-form-urlencoded方式提交数据。请求类似于下面这样:

代码语言:javascript
复制
POST http://www.example.com HTTP/1.1    Content-Type:
application/x-www-form-urlencoded;charset=utf-8
title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3

requests默认处理就是这种方式, exp:

代码语言:javascript
复制
url = 'http://httpbin.org/post'
d = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=d)
print r.text
2.multipart/form-data

除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单的类型为multipart/form-data。 这又是一个常见的 POST 数据提交的方式。我们使用表单上传文件时,必须让 form 的 enctyped 等于这个值:

requests exp:

代码语言:javascript
复制
from requests_toolbelt import MultipartEncoder
import requests

m = MultipartEncoder(
    fields={'field0': 'value', 'field1': 'value',
            'field2': ('filename', open('file.py', 'rb'), 'text/plain')}
    )

r = requests.post('http://httpbin.org/post', data=m,
                  headers={'Content-Type': m.content_type})
3.application/json

application/json 这个 Content-Type作为响应头大家肯定不陌生。实际上,现在越来越多的人把它作为请求头,用来告诉服务端消息主体是序列化后的 JSON 字符串。

requests exp:

代码语言:javascript
复制
url = 'http://httpbin.org/post'
s = json.dumps({'key1': 'value1', 'key2': 'value2'})
r = requests.post(url, data=s)
print r.text

or

代码语言:javascript
复制
requests.post(url='',data=json.dumps({'key1':'value1','key2':'value2'}),headers={'Content-Type':'application/json'})

or

代码语言:javascript
复制
requests.post(url='',json=key1,headers={'Content-Type':'application/json'})
4. text/xml

跟json类似,XML 作为编码方式的远程调用规范。

requests exp:

代码语言:javascript
复制
requests.post(url='',json=key1,headers={'Content-Type':'application/json'})
5. binary

直接二进制流数据传输,多用于上传图片

requests exp:

代码语言:javascript
复制
requests.post(url='',files={'file':open('test.xls','rb')},headers={'Content-Type':'binary'})

or

代码语言:javascript
复制
url = 'http://httpbin.org/post'
files = {'file': open('report.txt', 'rb')}
r = requests.post(url, files=files)
print r.text
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • requests库发送post请求的五种姿势;
  • 1.application/x-www-form-urlencoded
  • 2.multipart/form-data
  • 3.application/json
  • 4. text/xml
  • 5. binary
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档