很多教程喜欢忽略 HTTP 状态码,但真实项目里不能忽略。
from fastapi import FastAPI, status
app = FastAPI()
@app.post("/permits/", status_code=status.HTTP_201_CREATED)
async def create_permit(name: str):
return {"name": name}最常见的一组直觉:
200 OK -> 查到了 / 改成功了
201 Created -> 新建成功
204 No Content -> 删成功了,但不返回正文
400 Bad Request -> 请求本身有问题
401 Unauthorized -> 没带合法认证
403 Forbidden -> 你是谁我知道,但你没权限
404 Not Found -> 资源不存在
422 Unprocessable Content -> 数据格式合法但校验不过
500 Internal Server Error -> 服务端炸了