python中Requests发送json格式的post请求方法
作者:测试工程师Jane 时间:2021-05-24 10:09:45
前言
问题:
做requests请求时遇到如下报错:
{“code”:“500”,“message”:"JSON parse error: Cannot construct instance of com.bang.erpapplication.domain.User (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value
原因:
Requests.post源码如下:
post请求传body的参数有两种:data和json,那么我们来看一下python各种数据结构做为body传入的表现
1.普通string类型
string2 = "2222222"
r = requests.post("http://httpbin.org/post", data=string2)
print(r.text)
返回的结果:
2.string内是字典的
import requests
string = "{'key1': 'value1', 'key2': 'value2'}"
r = requests.post("http://httpbin.org/post", data=string)
print(r.text)
返回结果:
3.元组(嵌套列表或者)
import requests
string = (['key1', 'value1'],)
r = requests.post("http://httpbin.org/post", data=string)
print(r.text)
返回结果:
4.字典
5.json
import requests
import json
dic = {'key1': 'value1', 'key2': 'value2'}
string = json.dumps(dic)
r = requests.post("http://httpbin.org/post", data=string)
print(r.text)
返回结果:
6.传入非嵌套元组或列表
string = ['key1','value1']
r = requests.post("http://httpbin.org/post", data=string)
print(r.text)
返回报错:
7.以post(url,json=data)请求
dic = {'key1': 'value1', 'key2': 'value2'}
r = requests.post("http://httpbin.org/post", json=dic)
print(r.text)
运行结果:
由以上运行结果可以看出:
转入参数 | body数据类型 | headers(Content-type) |
---|---|---|
data | string | text/plain纯文本(默认) |
data | 元组(嵌套) | text/plain纯文本(默认)–转为dict |
data | 元组(非嵌套) | 报错,不支持 |
data | 列表 | 报错,不支持 |
data | 字典 | application/x-www-form-urlencoded(key/value表单) |
data | json(字符串但!= python string) | text/plain纯文本(默认)-要再做验证 |
json | 字典(源码内转成了json) | application/json(json串) |
现在让我们来看一下源码:
当转入json=data时:
当输入data=data时:
结论:
所以当你请求的data=dict时,未转为JSON的情况下,requests默认以表单形式key/value形式提交请求
setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
以json=dict形式请求时,以application/json格式发出请求
setRequestHeader("Content-type","application/json; charset=utf-8");
以data=其它请求时,默认就按纯文本格式请求:
setRequestHeader("Content-type", "text/plain; charset=utf-8");
来源:https://blog.csdn.net/totorobig/article/details/111071644
标签:python,Requests,post,son
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用ASP设计购物车
2008-04-17 13:52:00
彻底弄懂CSS盒子模式之三(浮动的表演和清除的自述)
2007-05-11 16:52:00
适合各种浏览器的js拖动层
2007-10-22 22:43:00
大内存SQL Server数据库的加速剂
2009-03-06 14:18:00
Golang使用ini库读取配置详情
2023-07-02 07:46:56
![](https://img.aspxhome.com/file/2023/5/89035_0s.webp)
select 终极美化
2007-10-16 17:57:00
飞扬远程获取类Asp xmlHttp 源码
2010-04-03 20:37:00
存储过程优缺点分析
2012-04-13 11:39:56
python实现矩阵乘法
2023-11-03 07:41:10
CSS写法性能
2009-05-28 19:09:00
![](https://img.aspxhome.com/file/UploadPic/20095/28/g2009521232840-47s.gif)
蚁群算法js版
2008-10-08 10:15:00
用Dreamweaver MX制作导航下拉菜单
2009-05-29 18:37:00
![](https://img.aspxhome.com/file/UploadPic/20072/200723113222576s.jpg)
游戏的用户体验营销小札
2009-08-30 15:13:00
XML入门教程:XML CDATA的作用
2007-11-07 14:08:00
Python实现壁纸下载与轮换
2022-07-01 19:19:19
python类属性学习深入讲解
2021-03-25 06:05:34
![](https://img.aspxhome.com/file/2023/7/66747_0s.png)
IOS苹果AppStore内购付款的服务器端php验证方法(使用thinkphp)
2023-06-14 13:05:56
请给PNG8一个机会
2009-09-16 14:22:00
Silverlight 3 Beta出来了
2009-03-19 13:17:00
ASP技巧:禁用清除页面缓存的五种方法
2009-07-21 12:33:00