Skip to content

Commit

Permalink
🐞 fix: 已知的一个影响程序的bug
Browse files Browse the repository at this point in the history
httpx 模块的 data 和 json 参数.有些服务器无法解码 json 数据只接受简单的表单,但是处理apis时把data都变成 dict 判断无意义。[stackoverflow](https://stackoverflow.com/questions/26685248/difference-between-data-and-json-parameters-in-python-requests-package)
  • Loading branch information
WhaleFell committed May 3, 2022
1 parent 3e3e38d commit 4dadd8b
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion flask_app/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding=utf8
import httpx
import json
# import requests
from .model import API, default_header


Expand All @@ -12,6 +12,10 @@ def test_resq(api: API, phone) -> httpx.Response:
"""
api = api.handle_API(phone)
with httpx.Client(headers=default_header, timeout=8) as client:
# 这个判断没意义.....但是我不知道怎么优化...
# https://stackoverflow.com/questions/26685248/difference-between-data-and-json-parameters-in-python-requests-package
# Todo: json 和 data 表单发送的问题,有些服务器不能解释 json,只能接受表单
# sol: 1. 添加额外字段判断...
if not isinstance(api.data, dict):
print("data")
resp = client.request(method=api.method, headers=api.header,
Expand All @@ -20,6 +24,16 @@ def test_resq(api: API, phone) -> httpx.Response:
print('json')
resp = client.request(
method=api.method, headers=api.header, url=api.url, json=api.data)

# 验证不是 httpx 的问题...
# if not isinstance(api.data, dict):
# print("data")
# resp = requests.request(method=api.method, headers=api.header,
# url=api.url, data=api.data)
# else:
# print('json')
# resp = requests.request(
# method=api.method, headers=api.header, url=api.url, json=api.data)
return resp


Expand Down

0 comments on commit 4dadd8b

Please sign in to comment.