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"}
相关推荐
风送雨1 天前
FastAPI 学习教程 · 第4部分
学习·fastapi
ghgxm5201 天前
FastApi_03_中间件 VS 依赖注入
java·中间件·fastapi
techzhi2 天前
Apifox CLI + GitLab CI:接口自动化测试实施记录
java·ci/cd·kubernetes·gitlab·yapi·运维开发·fastapi
梅羽落2 天前
fastapi速成2
python·github·fastapi
PD我是你的真爱粉2 天前
FastAPI 参数接收类型总结
fastapi
栗子叶2 天前
Spring 中 Servlet 容器和 Python FastAPI 对比
python·spring·servlet·fastapi
PD我是你的真爱粉2 天前
FastAPI基础
fastapi
曲幽2 天前
FastAPI搭档Pydantic:从参数验证到数据转换的全链路实战
python·fastapi·web·path·field·query·pydantic·basemodel·response_model
懒人村杂货铺3 天前
从 Permission Denied 到 404:Docker 多容器下图片上传与静态资源服务全解
docker·fastapi
勇气要爆发3 天前
AI后端工程化:FastAPI + Pydantic + JWT 鉴权实战,从零构建 AI 接口服务
人工智能·fastapi·jwt·pydantic