我正在构建一个简单的Flask API,我正在测试一个来自Postman的post请求,
这样地
{"name": "Frosty"}
.这是我的类,用于处理对POST请求所在端点的请求:
from http import HTTPStatus
from flask.views import MethodView
from flask import Blueprint
from injector import singleton, inject
from flask import jsonify, abort, request
@singleton
class PetsController(MethodView):
@inject
def __init__(self) -> None:
super().__init__()
self.pets = [
{"id": 1, "name": "Snoopy"},
{"id": 2, "name": "Furball"},
{"id": 3, "name": "Alaska"},
]
def configure(self):
self.pets_view = Blueprint("pets_view", __name__)
self.pets_view.add_url_rule("/pets/", view_func=PetsController.as_view("pets"))
def get(self):
return jsonify({"pets": self.pets})
def post(self):
data = request.get_json()
if not data or not "name" in data:
return jsonify(
message=f"Data missing from POST request {data}",
status=HTTPStatus.BAD_REQUEST.value,
)
new_pet = {"id": len(self.pets) + 1, "name": data["name"]}
self.pets.append(new_pet)
return jsonify(
message=f"new pet added: {new_pet}",
status=HTTPStatus.CREATED.value,
)
我收到了一个错误的请求响应,因为
request.get_json()
公元
request.json
两者都返回这个元组
(Ellipsis, Ellipsis)
.
知道为什么吗?