小编Oct*_*per的帖子

FastAPI:如何为特定路由自定义422异常?

如何仅针对 FastAPI 中的一条路由将 422 标准异常替换为自定义异常?

我不想替换应用项目,只想替换一条路线。我读了很多文档,但我不明白如何做到这一点。

我需要更改异常的路线示例422

from fastapi import APIRouter
from pydantic import BaseModel

router = APIRouter()


class PayloadSchema(BaseModel):
    value_int: int
    value_str: str


@router.post('/custom')
async def custom_route(payload: PayloadSchema):
    return payload
Run Code Online (Sandbox Code Playgroud)

python http-status-code-422 pydantic fastapi

5
推荐指数
1
解决办法
1819
查看次数

标签 统计

fastapi ×1

http-status-code-422 ×1

pydantic ×1

python ×1