FastAPI系列:如何改变响应状态码

FastAPI,顾名思义,是一个快速、现代、高性能的web框架,用于用Python构建后端api。响应状态码是一个三位数,表示请求的结果。例如,200表示OK, 404表示未找到,500表示服务器内部错误。默认情况下,FastAPI将为成功请求返回200状态码,为验证错误返回422状态码。

但是,有时你可能希望更改状态码以指示不同的结果。例如,你可能希望为创建新资源的成功POST请求返回201状态码,或者为无法找到所请求资源的GET请求返回404状态码。在这篇简明的基于示例的博文中,我将向你展示在FastAPI中更改响应状态代码的两种不同方法。

在这里插入图片描述

使用路由装饰器的status_code参数

要更改FastAPI中的响应状态代码,可以使用@app中的status_code参数。@app.get, @app.post 或其他路由装饰器。status_code参数接受来starlette.status模块的整数值或常量。例如,要为创建新用户的POST请求返回201状态码,你可以如下所示:

from fastapi import FastAPI
from starlette.status import HTTP_201_CREATED

# create the FastAPI instance
app = FastAPI()

# set the status code to 201
@app.post("/users", status_code=HTTP_201_CREATED)
def create_user(name: str):
    # some logic to create a new user
    return {"name": name}

在上面的代码片段中,我从starlette.status模块中导入了常量HTTP_201_CREATED。starlette.status模块是starlette框架的一部分,它是FastAPI的一个依赖。starlette.status模块为常见的HTTP状态码提供常量,如HTTP_200_OK、HTTP_404_NOT_FOUND等。使用这些常量可以使代码更具可读性,并避免拼写错误或错误。例如,我可以写status_code=HTTP_201_CREATED,而不是写status_code=201,这样更具描述性和清晰度。

运行下面命令重启服务:

uvicorn main:app --reload

然后去http://localhost:8000/docs, 可以看到文档中code的值与我们配置的一致,方便用户调试程序。

使用响应对象

在这种方法中,我们使用fastapi模块中的Response对象在函数体中动态设置状态码。Response对象有status_code属性,你可以从starlette.status模块中分配整数值或常量状态值。

下面的例子返回404状态码的GET请求,不能找到被请求的用户:

from fastapi import FastAPI, Response
from starlette.status import HTTP_404_NOT_FOUND, HTTP_200_OK

app = FastAPI()


@app.get("/users/{user_id}")
def get_user(user_id: int, response: Response):
    # some logic to get the user by id
    user = None

    if user is None:
        # set the status code
        response.status_code = HTTP_404_NOT_FOUND
        return {"detail": "User not found"}
    else:
        # set the status code
        response.status_code = HTTP_200_OK
        return user

查看文档,效果与上面一样。

完整实战案例

这是一个完整的代码示例,展示了如何在不同的场景下更改FastAPI中的响应状态代码:

# main.py

from fastapi import FastAPI, Response
from starlette.status import HTTP_201_CREATED, HTTP_404_NOT_FOUND

app = FastAPI()

# A mock database of users
users = [
    {"id": 1, "name": "Sling Academy"},
    {"id": 2, "name": "Ranni the Witch"},
    {"id": 3, "name": "Khue"},
]


# A helper function to get a user by id
def get_user_by_id(user_id: int):
    for user in users:
        if user["id"] == user_id:
            return user
    return None


# A POST route to create a new user and return a 201 status code
@app.post("/users", status_code=HTTP_201_CREATED)
def create_user(name: str):
    # some logic to create a new user and add it to the database
    new_user = {"id": len(users) + 1, "name": name}
    users.append(new_user)
    return new_user


# A GET route to get a user by id and return either a 200 or a 404 status code
@app.get("/users/{user_id}")
def get_user(user_id: int, response: Response):
    # some logic to get the user by id from the database
    user = get_user_by_id(user_id)
    if user is None:
        response.status_code = HTTP_404_NOT_FOUND
        return {"detail": "User not found"}
    else:
        return user

最后总结

在本文中,我解释了如何使用两种方法在FastAPI中更改响应状态代码:路由装饰器中的status_code参数和函数体中的response对象。你可以根据自己的需要和偏好使用任何一种方法。更改响应状态码可以帮助您更清楚地与客户端进行通信,并遵循RESTful API设计的最佳实践。

FastAPI 中,状态码的获取和设置通常通过 `response` 参数或返回 `Response` 对象来实现。以下是一些常用的方法和技巧: ### 获取响应状态码 1. **使用 `response` 参数控制状态码** 在定义路由时,可以通过 `response` 参数指定响应模型,并结合 `status_code` 来设置特定的状态码。例如,在处理成功请求时返回 `200 OK`,在发生错误时返回 `404 Not Found`。 ```python from fastapi import FastAPI, status from fastapi.responses import JSONResponse app = FastAPI() @app.get("/example") async def example_endpoint(): return JSONResponse(content={"message": "Success"}, status_code=status.HTTP_200_OK) ``` 2. **通过异常处理获取状态码** 使用 `HTTPException` 可以抛出带有特定状态码的异常,FastAPI 会自动捕获并返回对应的 HTTP 响应。 ```python from fastapi import FastAPI, HTTPException app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): if item_id not in items: raise HTTPException(status_code=404, detail="Item not found") return {"item_id": item_id} ``` 3. **自定义中间件中获取状态码** 在自定义的 `ResponseMiddleware` 中,可以通过拦截请求和响应来读取或修改状态码。例如,记录每个请求的响应状态码。 ```python from fastapi import Request, FastAPI from fastapi.middleware import Middleware from fastapi.middleware.base import BaseHTTPMiddleware class ResponseMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): response = await call_next(request) print(f"Response status code: {response.status_code}") # 获取状态码[^1] return response app = FastAPI() app.add_middleware(ResponseMiddleware) ``` 4. **直接操作 `Response` 对象** 如果需要更精细地控制响应内容和状态码,可以直接返回 `Response` 子类(如 `JSONResponse`, `PlainTextResponse` 等)的实例。 ```python from fastapi import FastAPI from fastapi.responses import Response app = FastAPI() @app.get("/custom-response") async def custom_response(): return Response(content="Custom content", status_code=201) # 自定义状态码[^2] ``` ### 示例:获取状态码的完整示例 ```python from fastapi import FastAPI, Request, Response, HTTPException from fastapi.middleware.base import BaseHTTPMiddleware class StatusLoggingMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): response = await call_next(request) print(f"Request path: {request.url.path}, Status Code: {response.status_code}") # 日志记录状态码[^1] return response app = FastAPI() app.add_middleware(StatusLoggingMiddleware) @app.get("/success") async def success(): return {"message": "OK"} # 默认返回 200 状态码 @app.get("/error") async def error(): raise HTTPException(status_code=400, detail="Bad Request") # 抛出 400 错误[^2] @app.get("/custom-status") async def custom_status(): return Response(content="Created", status_code=201) # 返回 201 状态码[^2] ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值