From 426a99d0b132bbcd0af0858d8805367ed821684c Mon Sep 17 00:00:00 2001 From: AdminWhaleFall Date: Wed, 4 May 2022 01:08:10 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20=E5=B7=B2=E7=9F=A5?= =?UTF-8?q?=E7=9A=84=E4=B8=80=E4=B8=AA=E5=BD=B1=E5=93=8D=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit httpx 模块的 data 和 json 参数.有些服务器无法解码 json 数据只接受简单的表单,但是处理apis时把data都变成 dict 判断无意义。[stackoverflow](https://stackoverflow.com/questions/26685248/difference-between-data-and-json-parameters-in-python-requests-package) --- flask_app/utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/flask_app/utils.py b/flask_app/utils.py index ffe3691..d828148 100644 --- a/flask_app/utils.py +++ b/flask_app/utils.py @@ -1,6 +1,6 @@ # encoding=utf8 import httpx -import json +# import requests from .model import API, default_header @@ -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, @@ -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