当前位置:网站首页>【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)
边栏推荐
- Super detailed tutorial for installing mysql8 in centos7 (no pit!)
- Record (II)
- What are MySQL clustered indexes and nonclustered indexes?
- Before we learn about high-performance computing, let's take a look at its history
- 很流行的状态管理库 MobX 是怎么回事?
- PHP pseudo protocol implementation command execution details
- Share some information I accumulated when I was working as a dotnet9 blog site
- SQL server queries are case sensitive
- The system needs to scan all the files and try to identify the cover of the video
- String analysis and use
猜你喜欢

(11) Tableview
![在D天内送达包裹的能力[抽象类二分--抽象判定方式]](/img/16/026c017e1c54d580fa2d237aa4605c.png)
在D天内送达包裹的能力[抽象类二分--抽象判定方式]

Introduction to ZigBee module wireless transmission star topology networking structure

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

C#使用S7.net连接西门子S1200PLC,C#直接连接西门子PLC

Exec function of PHP

OC swift hybrid

Kdd2022 | neural network compression of depth map based on antagonistic knowledge distillation

Abbexa AML1 DNA binding ELISA Kit instructions

Modify frontsortinglayer variable of spritemask
随机推荐
Array intersection of two arrays II
磁盘序列号,磁盘ID,卷序列号的区别
torch_geometric
Only this is the most true reason why leaders promote you. The rest is nonsense!
Solution to "XXX has broken path" error in idea
Detailed explanation of MySQL range query optimization scenario instances
php的exec函数
Install MySQL on Linux system. Problems encountered in xshell
Use of C language qsort() function (detailed explanation)
To do desktop plug-in, a good helper for office workers
Abbexa 8-OHdG CLIA kit solution
oc swift 混编
Latex error: file ‘xxx. sty‘ not found
sql server行转列(pivot),列转行(unpivot)
C程序实例1--个人通讯录管理系统
Interpreting the new ecology of education in maker space
C语言-排序中的快速排序(简称快排)
数组 两个数组的交集 II
笔记(五)- JVM
【MySQL】表结构的增删查改操作(DDL)