python requests data和json参数的区别

python requests data和json参数的区别

在通过request.post()进行POST请求时,传入报文的参数有两个,一个是data,一个是json
data与json 两者 分别由两种类型,一种是 str类型 一种是 dict 字典类型

那么两者之间有什么区别呢

  1. 不管是json还是str 还是dict,如果没有指定特定的请求头 headers中你的content-type,默认为 application/json
  2. data 为 dict 时,如果不指定content-type ,默认为普通表单的形式,比如not_checkout=1&spm=1000.2115.3001.4503&articleId=127684488这就是普通表单的形式,简单get请求 ,默认为application/x-www-form-urlencoded
  3. data 为 str时,如果不指定acontent-type more默认为text/plain
  4. json为 dict 时,如果不指定contenct-type 默认为application/json
  5. json为 str 时,如果不指定contenct-type 默认为application/json
  6. 用data参数提交数据时,request.body 的内容为a=1&b=2的形式,而用json参数提交数据时,{“a”:1,"b":2}的这种 key-value的形式

下面贴上具体代码,好好理解一下

>>> r = requests.post('http://httpbin.org/post', json = {'key':'value'})
>>> r.json()
{'args': {}, 'data': '{"key": "value"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '16', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.23.0', 'X-Amzn-Trace-Id': 'Root=1-5ecc83a5-6972ba8590410bc2c36d42df'}, 'json': {'key': 'value'}, 'origin': '61.148.199.18', 'url': 'http://httpbin.org/post'}
>>> r = requests.post('http://httpbin.org/post', json = json.dumps({'key':'value'}))
>>> r.json()
{'args': {}, 'data': '"{\\"key\\": \\"value\\"}"', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '22', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.23.0', 'X-Amzn-Trace-Id': 'Root=1-5ecc83b3-fd9620d07da4d72c64bb8b04'}, 'json': '{"key": "value"}', 'origin': '61.148.199.18', 'url': 'http://httpbin.org/post'}
>>> r = requests.post('http://httpbin.org/post', data = json.dumps({'key':'value'}))
>>> r.json()
{'args': {}, 'data': '{"key": "value"}', 'files': {}, 'form': {}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '16', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.23.0', 'X-Amzn-Trace-Id': 'Root=1-5ecc83c1-c5d88e82526fc52b94a51f00'}, 'json': {'key': 'value'}, 'origin': '61.148.199.18', 'url': 'http://httpbin.org/post'}
>>> r = requests.post('http://httpbin.org/post', data = {'key':'value'})
>>> r.json()
{'args': {}, 'data': '', 'files': {}, 'form': {'key': 'value'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '9', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.23.0', 'X-Amzn-Trace-Id': 'Root=1-5ecc83d0-09f85bff232d89cec4e5bdfb'}, 'json': None, 'origin': '61.148.199.18', 'url': 'http://httpbin.org/post'}
  

其中,介绍一下序列号和反序列化

json.dumps(data) 序列化 把字典格式的数据转换成str格式
json.loads(data)  反序列化 把str格式转换成字典格式
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值