当前位置:网站首页>【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)
边栏推荐
- Add, delete, query and modify MySQL table structure (DDL)
- Array intersection of two arrays II
- Abbexa 8-OHdG CLIA kit solution
- Interpreting the new ecology of education in maker space
- C中字符串查找
- ThinkPHP v6.0. X deserialization vulnerability recurrence
- I'm doing something cool
- ArrayList的扩容机制
- Several Apache related security vulnerability fixes
- Abbexa cell free DNA kit instructions
猜你喜欢

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

Mysql 什么是聚集索引和非聚集索引?

Mysql的回表查询?如何避免?

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

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

Abbexa 1,3-dipalmitonin CLIA kit solution
![Add, delete, query and modify [MySQL] table data (DML)](/img/08/4239bc0486fe8db2e98e54919300b5.png)
Add, delete, query and modify [MySQL] table data (DML)

Detailed explanation of Lora module wireless transceiver communication technology

JS mobile terminal copy text to clipboard code

Abbkine column exkine Pro animal cell / tissue Total Protein Extraction Kit
随机推荐
Array rotate array
Sum of two array numbers
记录(二)
Array plus one
I'm doing something cool
Abbexa cell free DNA kit instructions
Whether there are duplicate elements in the array
如何激发文化创新的活力和驱动力
[nk] 牛客月賽51 G計算題
Apple zoom! It's done so well
[debug] could not find ref wiht POC XXX
C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
Sealem Finance打造Web3去中心化金融平台基础设施
Ability to deliver packages within D days [abstract class dichotomy -- Abstract judgment method]
IDEA出现“XXX has broken path”报错解决方法
Constructing the implementation strategy of steam education for children
Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered
How to view the occupied space of a table in MySQL database
Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.