当 Flask 收到 POST 请求并尝试将其用作 JSON 数据时出错
Getting error when Flask gets a POST request and trying to use it as JSON data
我正在尝试从服务器获取 POST 请求,我希望我的服务器检查 JSON 中的数据是否等于我创建的哈希值。
我试过将 request.get_json(force=True)
更改为 request.json()
if request.method == 'POST':
request_json = request.get_json(force=True)
byte_key = 'REDACTED'
hashed_order = request_json['hashed_order']
message = payment_id.encode()
verification = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
if verification==payment_id:
return '', 200
else:
return abort(400)
还有POST要求:
POST callback_url | application/x-www-form-urlencoded
{
id: 'ba57e419-a6c9-41b2-a54c-b870d073d899',
callback_url: 'REDACTED',
success_url: 'REDACTED',
status: 'underpaid',
price: '250413',
fee: '0',
auto_settle: '0',
address: '2Mz4Sx2fmKpop4Hmi4jEtJhiuDCF9VVu2ds',
missing_amt: '125366',
hashed_order: 'c2a3896d4c8bfdcc25bbff0f3f15278fd948b96035f0438372eee9d4898f53b7'
}
无法解码 JSON 对象:预期值:第 1 行第 1 列(字符 0)
之间不匹配
application/x-www-form-urlencoded
并期望能够从请求中检索 JSON。为此,mime 类型必须是 application/json
(或 application/something+json
)
它包含在文档中。参见 https://flask.palletsprojects.com/en/1.1.x/api/
如果您好奇的话,https://github.com/pallets/werkzeug/blob/master/src/werkzeug/wrappers/json.py#L72 就是强制执行的地方。 (Flask 位于 Werkzeug 之上,它承担了大部分繁重的工作。)
我正在尝试从服务器获取 POST 请求,我希望我的服务器检查 JSON 中的数据是否等于我创建的哈希值。
我试过将 request.get_json(force=True)
更改为 request.json()
if request.method == 'POST':
request_json = request.get_json(force=True)
byte_key = 'REDACTED'
hashed_order = request_json['hashed_order']
message = payment_id.encode()
verification = hmac.new(byte_key, message, hashlib.sha256).hexdigest()
if verification==payment_id:
return '', 200
else:
return abort(400)
还有POST要求:
POST callback_url | application/x-www-form-urlencoded
{
id: 'ba57e419-a6c9-41b2-a54c-b870d073d899',
callback_url: 'REDACTED',
success_url: 'REDACTED',
status: 'underpaid',
price: '250413',
fee: '0',
auto_settle: '0',
address: '2Mz4Sx2fmKpop4Hmi4jEtJhiuDCF9VVu2ds',
missing_amt: '125366',
hashed_order: 'c2a3896d4c8bfdcc25bbff0f3f15278fd948b96035f0438372eee9d4898f53b7'
}
无法解码 JSON 对象:预期值:第 1 行第 1 列(字符 0)
application/x-www-form-urlencoded
并期望能够从请求中检索 JSON。为此,mime 类型必须是 application/json
(或 application/something+json
)
它包含在文档中。参见 https://flask.palletsprojects.com/en/1.1.x/api/
如果您好奇的话,https://github.com/pallets/werkzeug/blob/master/src/werkzeug/wrappers/json.py#L72 就是强制执行的地方。 (Flask 位于 Werkzeug 之上,它承担了大部分繁重的工作。)