python request要求接口参数必须是json数据的处理方式
作者:Jason_WangYing 时间:2023-07-14 03:12:18
Reqeusts支持以form表单形式发送post请求,只需要将请求的参数构造成一个字典,然后传给requests.post()的data参数即可。
data参数的格式如下:(content-type在header中设置)
1、data为dict时,如果不指定content-type,默认为application/x-www-form-urlencoded,相当于普通form表单提交的形式
2、data为str时,如果不指定content-type,默认为text/plain
3、json为dict时,如果不指定content-type,默认为application/json
4、json为str时,如果不指定content-type,默认为application/json
额外说下
application/x-www-form-urlencoded form表单,会对数据进行unicode编码,在发送前编码所有字符(默认)
multipart/form-data 不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
text/plain 文本形式 ,空格转换为 “+” 加号,但不对特殊字符编码。
application/json 作为请求头告诉服务端消息主体是序列化的JSON字符串。除低版本的IE,基本都支持
所以如果想提交json数据,可以使用如下方式
使用headers声明下,我这个数据是需要进行json解析的
post_headers = {'Content-Type': 'application/json'}
res = requests.post(url, headers=post_headers, data=json.dumps(data), verify=False)
也可以时间用json来传递
res = requests.post(url, json=data, verify=False)
来源:https://blog.csdn.net/Jason_WangYing/article/details/126127684
标签:python,request,接口参数,json,数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python利用pandas处理Excel数据的应用详解
2022-02-08 16:25:02
![](https://img.aspxhome.com/file/2023/6/82546_0s.png)
Python手绘可视化工具cutecharts使用实例
2022-12-25 07:27:54
![](https://img.aspxhome.com/file/2023/3/121403_0s.png)
Python使用pyodbc访问数据库操作方法详解
2021-08-02 19:12:29
MYSQL启用日志和查看日志
2010-12-03 16:24:00
Python编程使用tkinter模块实现计算器软件完整代码示例
2022-07-17 06:31:07
![](https://img.aspxhome.com/file/2023/5/95595_0s.jpg)
Go语言中的匿名结构体用法实例
2023-07-07 11:10:21
常用于后台开发的jQuery插件
2010-09-25 12:47:00
详解Python为什么不用设计模式
2022-03-30 16:17:12
![](https://img.aspxhome.com/file/2023/4/133874_0s.png)
Tensorflow 实现线性回归模型的示例代码
2023-07-03 09:00:41
![](https://img.aspxhome.com/file/2023/6/61166_0s.jpg)
python代码实现五子棋游戏
2021-04-18 01:20:48
![](https://img.aspxhome.com/file/2023/6/112586_0s.jpg)
Python进程间通信用法实例
2023-02-13 02:53:18
Python中re模块:匹配开头/结尾(^/$)
2021-08-31 02:29:13
Python configparser模块配置文件过程解析
2023-03-04 09:35:49
Server 对象 错误 ASP 0177 800401f3 的解决方案 Server 对象 错误 ASP 0177 800401f3
2009-07-28 17:57:00
python的等深分箱实例
2022-11-30 01:01:20
![](https://img.aspxhome.com/file/2023/2/128532_0s.jpg)
如何在Python项目中引入日志
2023-01-25 15:10:53
![](https://img.aspxhome.com/file/2023/7/67757_0s.png)
六个Python编程最受用的内置函数使用详解
2022-06-12 22:26:01
利用Python实现RSA加密解密方法实例
2023-10-07 03:10:58
![](https://img.aspxhome.com/file/2023/7/130997_0s.png)
Python计算机视觉里的IOU计算实例
2021-12-07 18:15:10
java 截取字符串(判断汉字)
2023-06-29 23:38:19