码迷,mamicode.com
首页 > Windows程序 > 详细

FastAPI 快速搭建一个REST API 服务

时间:2020-05-22 00:02:10      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:agg   read   def   pre   mod   docker   href   测试   github   

最近正好在看好的接口文档工具

快速示例

from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn

app = FastAPI()

class Item(BaseModel):
    name: str
    price: float
    is_offer: bool = None

@app.get("/")
def read_root():
    return {"Hello": "FastAPI"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q }

# 升级示例
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
    return {"item_name": item.name, "item_id": item_id}

if __name__ == ‘__main__‘:
    uvicorn.run("main:app", port=5000, reload = True )

启动访问

Docker 部署

TODO

官网

FastAPI 快速搭建一个REST API 服务

标签:agg   read   def   pre   mod   docker   href   测试   github   

原文地址:https://www.cnblogs.com/52liming/p/12933955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!