主页 > 互联网  > 

FastAPI学习-27使用@app.api_route()设置多种请求方式

FastAPI学习-27使用@app.api_route()设置多种请求方式

对同一个访问函数设置多个http 请求方式

api_route 使用

使用methods 参数设置请求方式

from fastapi import FastAPI app = FastAPI() @app.api_route('/demo/b', methods=['get', 'post']) async def demo2(): return {"msg": "demo2 success"}

判断请求方式执行不同内容

判断请求方式,执行不同内容

@app.api_route('/m', methods=['get', 'post']) async def view_methods(request: Request): if request.method == 'GET': return {"msg": "get demo2 success"} if request.method == 'POST': return {"msg": "post demo2 success"} return {"msg": "demo2 success"}
标签:

FastAPI学习-27使用@app.api_route()设置多种请求方式由讯客互联互联网栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“FastAPI学习-27使用@app.api_route()设置多种请求方式