StatusCode響應狀態碼,可用在GET/POST/PUT/DELETE方法中。
from fastapi import FastAPI
app = FastAPI()
@app.post("/items/", status_code=201)
async def create_item(name: str):
return {"name": name}
其中,status_code既可以爲數字1**/2**/3**/4**/5**,也可以通過導入status枚舉
from fastapi import FastAPI, status
app = FastAPI()
@app.post("/items/", status_code=status.HTTP_201_CREATED)
async def create_item(name: str):
return {"name": name}
完整的http代碼含義
1** 信息,服務器收到請求,需要請求者繼續執行操作
2** 成功,操作被成功接收並處理
3** 重定向,需要進一步的操作以完成請求
4** 客戶端錯誤,請求包含語法錯誤或無法完成請求
5** 服務器錯誤,服務器在處理請求的過程中發生了錯誤
https://www.runoob.com/http/http-status-codes.html
參考資料
- FastAPI官網 :https://fastapi.tiangolo.com/
- FastAPI源碼 :https://github.com/tiangolo/fastapi