回應標頭¶
使用 Response 參數¶
你可以在你的路徑操作函式(path operation function)中宣告一個 Response 型別的參數(就像處理 Cookie 一樣)。
然後你可以在那個暫時性的 Response 物件上設定標頭。
from fastapi import FastAPI, Response
app = FastAPI()
@app.get("/headers-and-object/")
def get_headers(response: Response):
response.headers["X-Cat-Dog"] = "alone in the world"
return {"message": "Hello World"}
接著你可以像平常一樣回傳任何你需要的物件(dict、資料庫模型等)。
如果你宣告了 response_model,它仍會用來過濾並轉換你回傳的物件。
FastAPI 會使用那個暫時性的回應來擷取標頭(還有 Cookie 與狀態碼),並把它們放到最終回應中;最終回應包含你回傳的值,且會依任何 response_model 進行過濾。
你也可以在依賴中宣告 Response 參數,並在其中設定標頭(與 Cookie)。
直接回傳 Response¶
當你直接回傳 Response 時,也能加入標頭。
依照直接回傳 Response中的說明建立回應,並把標頭作為額外參數傳入:
from fastapi import FastAPI
from fastapi.responses import JSONResponse
app = FastAPI()
@app.get("/headers/")
def get_headers():
content = {"message": "Hello World"}
headers = {"X-Cat-Dog": "alone in the world", "Content-Language": "en-US"}
return JSONResponse(content=content, headers=headers)
技術細節
你也可以使用 from starlette.responses import Response 或 from starlette.responses import JSONResponse。
為了方便開發者,FastAPI 提供與 starlette.responses 相同的內容於 fastapi.responses。但大多數可用的回應類型其實直接來自 Starlette。
由於 Response 常用來設定標頭與 Cookie,FastAPI 也在 fastapi.Response 提供了它。
自訂標頭¶
請記住,專有的自訂標頭可以使用 X- 前綴來新增。
但如果你有自訂標頭並希望瀏覽器端的客戶端能看見它們,你需要把這些標頭加入到 CORS 設定中(詳見 CORS(跨來源資源共用)),使用在Starlette 的 CORS 文件中記載的 expose_headers 參數。