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