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"}
相关推荐
趣谈AI14 小时前
使用Trae编辑器开发Python Api (FastApi 框架)
python·编辑器·fastapi
Amd7944 天前
FastAPI依赖注入性能优化策略
单例模式·性能优化·fastapi·依赖注入·错误处理·异步编程·缓存机制
Amd7945 天前
FastAPI安全认证中的依赖组合
测试用例·fastapi·jwt·依赖注入·权限校验·安全认证·组合依赖
勘察加熊人6 天前
vue+d3js+fastapi实现天气柱状图折线图饼图
前端·vue.js·fastapi
Amd7948 天前
FastAPI中的依赖注入与数据库事务管理
fastapi·依赖注入·sqlalchemy·事务管理·异步编程·sql注入防护·数据库会话管理
背太阳的牧羊人9 天前
app = Flask(__name__)和app = FastAPI()有什么区别和联系
python·flask·fastapi
Amd7949 天前
FastAPI依赖注入作用域与生命周期控制
fastapi·作用域·依赖注入·资源管理·请求级作用域·应用级作用域·生命周期控制
老大白菜10 天前
FastAPI-Cache2: 高效Python缓存库
python·缓存·fastapi
老大白菜11 天前
Python FastAPI + Celery + RabbitMQ 分布式图片水印处理系统
分布式·python·fastapi
Amd79411 天前
FastAPI依赖注入实践:工厂模式与实例复用的优化策略
单例模式·性能优化·fastapi·工厂模式·依赖注入·多租户系统·实例复用