Fastapi学习日记

from fastapi import FastAPI
app = FastAPI()

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


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

运行服务器:

uvicorn main:app --reload

命令uvicorn main:app --reload指的是:

  • main:main.py文件(
  • app:app = FastAPI() 在main.py内创建的对象。
  • --reload:在代码更改后重新启动服务器。 只有在开发时才使用这个参数。

在你的浏览器打开网址: http://127.0.0.1:8000
 
  • url: / 和 / items / {item_id},两个url都可以接收HTTP请求。
  • / 和 / items / {item_id} 都采用GET方式的HTTP请求方法
  • / items / {item_id}包含路径参数item_id,格式为int
  • / items / {item_id}还包含一个可选的参数q,格式为str
  http://127.0.0.1:8000/docs #交互文档
  http://127.0.0.1:8000/redoc #备用交互文档

下载API接口文档

如果你需要提供你的API接口,那么只需要一行命令,即可下载api文件,一般保存为api.json

curl -o api.json http://127.0.0.1:8000/openapi.json

  

 

猜你喜欢

转载自www.cnblogs.com/smilegg/p/13186956.html