本系列汇总,请查看这里https://www.cnblogs.com/uncleyong/p/19503695

路径操作装饰器方法

也就是请求方式,fastapi支持各种请求方式:

@app.get()
@app.post()
@app.put()
@app.patch()
@app.delete()
@app.options()
@app.head()
@app.trace()

示例

from fastapi import FastAPI
import uvicorn

app = FastAPI()


@app.get("/get")
def get_test():
    return {"method": "get方法"}


@app.post("/post")
def post_test():
    return {"method": "post方法"}


@app.put("/put")
def put_test():
    return {"method": "put方法"}


@app.delete("/delete")
def delete_test():
    return {"method": "delete方法"}


if __name__ == '__main__':
    uvicorn.run("test_method:app", port=8001, reload=True)

启动服务后访问:http://127.0.0.1:8001/docs#/

image

路径操作装饰器方法参数

参数很多,下面先介绍和接口文档相关的几个参数

image

示例

from fastapi import FastAPI
import uvicorn

app = FastAPI()


@app.get("/get", tags=["这是/get接口"],
          summary="this is /get接口 summary",
          description="this is /get接口 description...",
          response_description="this is /get接口 response_description...",
          )
def get_test():
    return {"method": "get方法"}


@app.post("/post", tags=["这是/post接口"],
          summary="this is /post接口 summary",
          description="this is /post接口 description...",
          response_description="this is /post接口 response_description...",
          deprecated=True,
          )
def post_test():
    return {"method": "post方法"}


@app.put("/put")
def put_test():
    return {"method": "put方法"}


@app.delete("/delete")
def delete_test():
    return {"method": "delete方法"}


if __name__ == '__main__':
    uvicorn.run("test_method:app", port=8001, reload=True)

接口文档中,废弃的置灰了

image

 其它几个参数值展示位置

image

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐