当前位置:网站首页>【Py】接口签名校验失败可能是由于ensure_ascii的问题
【Py】接口签名校验失败可能是由于ensure_ascii的问题
2022-06-10 21:15:00 【micromicrofat】
项目场景:
客户端:requests
服务端:Flask
客户端发请求前对请求签名,服务端对签名进行校验
问题描述
服务端签名校验失败,即生成的签名与客户端的不一致
原因分析:
假设我们的请求体为
data = {
"a": "你好"
}
在对字典数据进行json序列化时使用了如下代码
json.dumps(data, ensure_ascii=False)
其中的ensure_ascii参数是控制是否在序列化时使用ascii编码,默认为使用。效果就是中文字符全部变为unicode编码,这是因为中文字符不在ascii编码中。
'{"a": "\\u4f60\\u597d"}'
json序列化后对其进行签名,得到signature
再通过requests将请求发送出去
requests('http://xx.xx.xx.xx', json=data)
可能你已经发现问题了:requests中的json=data默认是使用ascii编码的,所以这就导致服务端进行签名时使用的请求体与客户端的不一样,签名必然失败(有非ascii字符时)
解决方案:
统一签名和发送请求时的序列化规则即可
json.dumps(data, ensure_ascii=False)
requests(..., data=json.dumps(data, ensure_ascii=False).encode('utf-8'))
或
json.dumps(data, ensure_ascii=True)
requests(..., json=data)
边栏推荐
- 【phpstorm】 No data sources are configured to run this SQL and provide advanced c
- Visio to high quality pdf
- 字符串 反转
- Array union set
- C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC
- Sum of two array numbers
- Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
- How to stimulate the vitality and driving force of cultural innovation
- Detailed explanation of MySQL range query optimization scenario instances
- Pytorch 安装超简单
猜你喜欢

2022 - 06 - 09 rk817 PMU Battery Temperature Detection
![Ajout, suppression et modification des données du tableau [MySQL] (DML)](/img/08/4239bc0486fe8db2e98e54919300b5.png)
Ajout, suppression et modification des données du tableau [MySQL] (DML)

If else is too easy to use? (understanding this article will further improve your logic)

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas

Detailed explanation of Lora module wireless transceiver communication technology

AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
![[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas](/img/00/798455f2db289e3aaed36981ea1e7c.png)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas

很流行的状态管理库 MobX 是怎么回事?

SQL第四练:字符串处理函数

php的exec函数
随机推荐
Intelligent robot making progress in imitation learning
Add, delete, query and modify MySQL table structure (DDL)
Cordova plugin /jpush phonegap Aurora push_ Local push_ Message push
The system needs to scan all the files and try to identify the cover of the video
修改SpriteMask 的 frontSortingLayer 变量
Detailed explanation of MySQL range query optimization scenario instances
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
Share this great God's WPF interface design series video
【MySQL】表数据的增删查改(DML)
[1024 ways to play windows azure] 75 Fast cloud database migration seamlessly migrate alicloud RDS SQL server to azure SQL databas
PMP candidates, there are these places for PMP examination in Shenzhen in June 2022
Abbexa 1,3-dipalmitonin CLIA kit solution
JS mobile terminal copy text to clipboard code
Icml2022 | sharp maml: model independent meta learning for sharpness perception
数组 求上升区间的高度和
A number that appears only once in an array
Array remove duplicates from an array
Array rotates the array from bits of the specified length
Mysql的回表查询?如何避免?
2022-06-09 rk817 PMU battery temperature detection