fastapi入门一简单的get请求
依赖包准备
pip3 install uvicorn
pip3 install fastapi
get请求
"""
@File : fastapiOne.py
@Desciption:
"""
import uvicorn
from fastapi import FastAPI
app = FastAPI()
# app = FastAPI() 用于实例化 FastAPI类,通常app这个变量会设置成全局的,至少在当前模块作用域有效。
@app.get("/fastapi")
# 是一个装饰器,在这里的作用是将普通的Python函数变成一个get请求接口
async def root():
dic = {"message": "hello fastapi"}
return dic
if __name__ == "__main__":
uvicorn.run(app='fastapiOne:app', host='127.0.0.1', port=8100, reload=True, debug=True)
# app='fastapiOne:app' 这个fastapiOne为文件的名字
# reload=True的意思是文件内容变更后自动重启服务,便于开发调试。
发起请求
启动服务后控制台信息
INFO: Will watch for changes in these directories: ['/Users/zc/PycharmProjects/fastapitest/One']
INFO: Uvicorn running on http://127.0.0.1:8100 (Press CTRL+C to quit)
INFO: Started reloader process [87917] using statreload
INFO: Started server process [87920]
INFO: Waiting for application startup.
INFO: Application startup complete.
请求以下地址
http://127.0.0.1:8100/fastapi
查看接口文档
打开以下地址
http://127.0.0.1:8100/docs
查看API文档
http://127.0.0.1:8100/redoc