可以在验证错误上获得正确的服务器响应(Spectree+ Blask)

发布于 2025-02-12 05:41:33 字数 1569 浏览 1 评论 0原文

我使用Spectree在烧瓶上的API有问题。 我有一个端点,其中一个“帖子”方法,该方法获取一些数据,验证它,如果成功,请返回{“ status”:“ ok”},如果发生验证错误,请返回400代码和一条消息,即存在验证错误。这不是我拥有的,而是我想实现的目标。我建议您查看代码以更好地理解我的意思:

from flask import Flask
from spectree import SpecTree, Response
from pydantic import BaseModel

class Error(BaseModel):
    code:int = 400
    message:str = 'Validation Failed'

class Unit(BaseModel):
    id: int

app = Flask(__name__)
api = SpecTree('flask', app=app, title='API', version='v1.0', path='docs')

@app.post('/imports')
@api.validate(json=Unit, validation_error_status=400, resp=Response(HTTP_400=Error))
def imports():
    return {'status': 'OK'}, 200

也就是说,您可以看到,在文档之后,我设置vilastion_error_status = 400,以便当验证失败时,客户端会收到响应使用400代码(哇?)。我还设置了revest = reverse(http_400 = error),以便当发生400个错误时,客户端会收到错误响应。因此,在验证错误时,API客户端应接收到与错误模型相对应的响应(请参见下文)

,如果我发送无效的请求,也就是说,id不会是int ,但例如“ fkfld”,我会收到以下答复:

[
  {
    "loc": [
      "id"
    ],
    "msg": "value is not a valid integer",
    "type": "type_error.integer"
  }
]

我认为答案是:

{
  "code": 400,
  "message": "Validation Failed"
}

我使用:

  • python 3.8.3
  • 烧瓶2.1.2
  • 频谱0.10.0

我很难理解我的错误是什么,但是我不能。我已经阅读了文档,但是我的问题仅与:

什么是响应以及如何使用?

要为端点构建响应,您需要使用格式http_ {code}和相应的数据(可选)声明状态代码。

请帮助我弄清楚这一点,我真的很想了解我的错误以及如何正确地做。

I have a problem with my API on Flask, using spectree.
I have one endpoint with one "POST" method, which takes some data, validates it, and if successful, returns {"status": "OK"}, and in case of validation error, returns a 400 code and a message that there was a validation error. It is rather not what I have, but what I wanted to achieve. I suggest taking a look at the code to better understand what I mean:

from flask import Flask
from spectree import SpecTree, Response
from pydantic import BaseModel

class Error(BaseModel):
    code:int = 400
    message:str = 'Validation Failed'

class Unit(BaseModel):
    id: int

app = Flask(__name__)
api = SpecTree('flask', app=app, title='API', version='v1.0', path='docs')

@app.post('/imports')
@api.validate(json=Unit, validation_error_status=400, resp=Response(HTTP_400=Error))
def imports():
    return {'status': 'OK'}, 200

That is, as you can see, following the documentation, I set validation_error_status=400 so that when validation fails, the client receives a response with a 400 code (wow?). I also set resp=Response(HTTP_400=Error) so that when a 400 error occurs, the client receives an Error response. Thus, on a validation error, the API client should receive a response corresponding to the Error model (see below)

Now, if I send a request that is not valid, that is, id will not be int, but for example "fkfld", I will receive the following response:

[
  {
    "loc": [
      "id"
    ],
    "msg": "value is not a valid integer",
    "type": "type_error.integer"
  }
]

I thought the answer would be:

{
  "code": 400,
  "message": "Validation Failed"
}

I use:

  • Python 3.8.3
  • Flask 2.1.2
  • spectree 0.10.0

I tried very hard to understand what my mistake was, but I could not. I've read the documentation, but my problem is only related to:

What is Response and how to use it?

To build a response for the endpoint, you need to declare the status code with format HTTP_{code} and corresponding data (optional).

Please help me figure this out, I really want to understand my mistakes and how to do it right.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文