python fastapi 示例


源代碼
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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM