FastAPI Response(四) 高级定制的Response
作者:麦克煎蛋 出处:https://www.cnblogs.com/mazhiyong/ 转载请保留这段声明,谢谢!
我们可以更灵活的定制返回结果。(这部分是相对高级的内容,如果你是初次接触FastAPI那么可以忽略)
参数responses
我们可以传递给路径操作装饰器一个参数responses
,他接收一个字典数据,键值是不同的HTTP状态码,内容是不同情况下的返回内容(字典格式)。
如果返回内容包含键值model,那么它的作用与response_model
相同,指向的内容是Pydantic模型。
如下示例,当返回状态码是404的时候,对应的Pydantic模型是Message:
from fastapi import FastAPI from fastapi.responses import JSONResponse from pydantic import BaseModel class Item(BaseModel): id: str value: str class Message(BaseModel): message: str app = FastAPI() @app.get("/items/{item_id}", response_model=Item, responses={404: {"model": Message}}) async def read_item(item_id: str): if item_id == "foo": return {"id": "foo", "value": "there goes my hero"} else: return JSONResponse(status_code=404, content={"message": "Item not found"})
在上面的示例中,正常情况下返回的数据模型是Item,404的时候返回的数据模型是Message。
不同的media type
参数responses也支持不同的media type。
from typing import Optional from fastapi import FastAPI from fastapi.responses import FileResponse from pydantic import BaseModel class Item(BaseModel): id: str value: str app = FastAPI() @app.get( "/items/{item_id}", response_model=Item, responses={ 200: { "content": {"image/png": {}}, "description": "Return the JSON item or an image.", } }, ) async def read_item(item_id: str, img: Optional[bool] = None): if img: return FileResponse("image.png", media_type="image/png") else: return {"id": "foo", "value": "there goes my hero"}
如上所示,默认的media type是application/json
,同时还支持image/png
。
返回信息的组合格式
我们可以合并不同的组合信息,其中可以包含response_model
, status_code
, 以及 responses
等参数。
from fastapi import FastAPI from fastapi.responses import JSONResponse from pydantic import BaseModel class Item(BaseModel): id: str value: str class Message(BaseModel): message: str app = FastAPI() @app.get( "/items/{item_id}", response_model=Item, responses={ 404: {"model": Message, "description": "The item was not found"}, 200: { "description": "Item requested by ID", "content": { "application/json": { "example": {"id": "bar", "value": "The bar tenders"} } }, }, }, ) async def read_item(item_id: str): if item_id == "foo": return {"id": "foo", "value": "there goes my hero"} else: return JSONResponse(status_code=404, content={"message": "Item not found"})
合并预定义responses与自定义responses
from typing import Optional from fastapi import FastAPI from fastapi.responses import FileResponse from pydantic import BaseModel class Item(BaseModel): id: str value: str responses = { 404: {"description": "Item not found"}, 302: {"description": "The item was moved"}, 403: {"description": "Not enough privileges"}, } app = FastAPI() @app.get( "/items/{item_id}", response_model=Item, responses={**responses, 200: {"content": {"image/png": {}}}}, ) async def read_item(item_id: str, img: Optional[bool] = None): if img: return FileResponse("image.png", media_type="image/png") else: return {"id": "foo", "value": "there goes my hero"}