源代碼
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
app = FastAPI() #實例化FastAPI
class Item(BaseModel):
name: str
price: float
is_offer: bool = None
'''
{
"named": "string",
"price": 0,
"is_offer": true
}
'''
@app.get("/")
async def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None): # 此處q為query的字段
return {"item_id": item_id, "q": q}
@app.post("/items/{item_id}")
async def update_item(item_id: int, item: Item): # 此處Item為body的schema
return {"item_name": item.name, "item_id": item_id}
if __name__ == '__main__':
uvicorn.run('main:app',port=8080)