【发布时间】:2021-11-24 06:08:26
【问题描述】:
我的烧瓶应用程序,它在 docker 容器中工作,没有收到 json 请求。 当我发送 json 请求时,我得到元组作为响应,看起来像这样(省略号,省略号)
我的端点
class WhoisInfo(Resource):
def post(self):
if request.method == "POST":
logger.debug("got request method POST")
if request.is_json:
logger.debug("is json")
logger.debug(request.args.get('host'))
logger.debug(request.get_data())
return {"error": "Not information about this zone "}, 400
api_v1.add_resource(WhoisInfo, "/api/whois/")
调试输出
backend_1 | 2021-10-03 13:03:30.582 | DEBUG | api.views:post:53 - got request method POST
backend_1 | 2021-10-03 13:03:30.583 | DEBUG | api.views:post:55 - is json
backend_1 | 2021-10-03 13:03:30.583 | DEBUG | api.views:post:56 - (Ellipsis, Ellipsis)
backend_1 | 2021-10-03 13:03:30.583 | DEBUG | api.views:post:57 - b'{"host": "8.8.8.8"}'
我的请求
import requests
url = "http://127.0.0.1:80/api/whois/"
payload={"host": "8.8.8.8"}
headers = {
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data=payload)
【问题讨论】:
-
而不是
data=payload使用json=payload并删除您的自定义标题。然后在你看来使用request.json。 -
我做到了,但错误是一样的
标签: python flask flask-restful