建立API
由URL路徑取得參數
from fastapi import FastAPI
app = FastAPI()
@app.get("/item/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id}由Query取得參數
from fastapi import FastAPI
app = FastAPI()
@app.get("/items/")
def read_item(skip: int = 0, limit: int = 10):
return {"data": [skip, limit]}http://127.0.0.1:8000/items/?skip=0&limit=10使用Pydantic
自動生成API文件
FastAPI備受廣大後端工程師喜愛的特點就是能夠自動生成Swagger UI的API文件,大大提升開發者的效率與歡樂度,只要在網頁瀏覽器中輸入http://127.0.0.1:8000/docs 就可以看到API文件了。
Last updated