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

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

api_route 使用

使用methods 参数设置请求方式

python 复制代码
from fastapi import FastAPI
  
  
app = FastAPI()  


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

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

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

python 复制代码
@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"}
相关推荐
jingyucsdn6 小时前
网页端 VUE+C#/FastAPI获取客户端IP和hostname
网络协议·tcp/ip·fastapi
掘金-我是哪吒2 天前
分布式微服务系统架构第144集:FastAPI全栈开发教育系统
分布式·微服务·架构·系统架构·fastapi
jingyucsdn8 天前
AsyncIOScheduler与BackgroundScheduler的线程模型对比
fastapi
jingyucsdn9 天前
FastAPI集成APsecheduler的BackgroundScheduler+mongodb(精简)
fastapi
几道之旅9 天前
前端antd,后端fastapi,解决文件上传
前端·fastapi
Hello_WOAIAI10 天前
python中使用高并发分布式队列库celery的那些坑
python·fastapi
ZHOU_WUYI12 天前
FastAPI在 Nginx 和 Docker 环境中的部署
nginx·docker·fastapi
王学政212 天前
FastAPI 中间件
fastapi
小迅先生15 天前
AI开发 | Web API框架选型-FastAPI
开发语言·python·fastapi
老大白菜17 天前
双均线量化交易策略指南
python·fastapi