如何使用Python的Requests库发送JSON格式的POST请求?
- 内容介绍
- 相关推荐
本文共计662个文字,预计阅读时间需要3分钟。
目录前言
1.常见string类型
2.string内是字符串
3.元组(嵌套列表或)
4.字典
5.json
6.传入非嵌套数组或对象
7.使用post(url, json=data)请求
问题:做requests请求时遇到如下报错:
目录
- 前言
- 1.普通string类型
- 2.string内是字典的
- 3.元组(嵌套列表或者)
- 4.字典
- 5.json
- 6.传入非嵌套元组或列表
- 7.以post(url,json=data)请求
前言
问题:
做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("httpbin.org/post", json=dic) print(r.text)
运行结果:
由以上运行结果可以看出:
现在让我们来看一下源码:
当转入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");
到此这篇关于python中Requests发送json格式的post请求实操的文章就介绍到这了,更多相关python post请求内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!
本文共计662个文字,预计阅读时间需要3分钟。
目录前言
1.常见string类型
2.string内是字符串
3.元组(嵌套列表或)
4.字典
5.json
6.传入非嵌套数组或对象
7.使用post(url, json=data)请求
问题:做requests请求时遇到如下报错:
目录
- 前言
- 1.普通string类型
- 2.string内是字典的
- 3.元组(嵌套列表或者)
- 4.字典
- 5.json
- 6.传入非嵌套元组或列表
- 7.以post(url,json=data)请求
前言
问题:
做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("httpbin.org/post", json=dic) print(r.text)
运行结果:
由以上运行结果可以看出:
现在让我们来看一下源码:
当转入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");
到此这篇关于python中Requests发送json格式的post请求实操的文章就介绍到这了,更多相关python post请求内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

