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"}
相关推荐
CaffeinePro2 天前
依赖注入:FastAPI最核心的解耦能力案例解析
后端·fastapi
曲幽5 天前
刚部署的 LibreTranslate 频频翻车?我掏出了 20 年前的 StarDict 词典,用 FastAPI 搭了个本地词典翻译 API
python·fastapi·web·translate·goldendict·libretranslate·stardict·pystardict
CaffeinePro9 天前
Pydantic深度使用:数据校验、枚举、ORM映射
后端·fastapi
jay神11 天前
基于 FastAPI + Vue 的宠物领养管理系统
前端·vue.js·python·毕业设计·fastapi·宠物
染指111012 天前
6.AI大模型-搭建本地大模型服务体系
fastapi·oneapi
codeaideaai13 天前
使用UV创建python项目
python·fastapi·uv
放下华子我只抽RuiKe513 天前
FastAPI 全栈后端(八):部署与运维
运维·数据库·react.js·oracle·数据挖掘·前端框架·fastapi
SilentSamsara13 天前
模型部署实战:FastAPI + ONNX + Docker 的推理服务化
人工智能·pytorch·python·深度学习·机器学习·fastapi
放下华子我只抽RuiKe513 天前
FastAPI 全栈后端(七):测试与自动化
运维·前端·人工智能·react.js·前端框架·自动化·fastapi
abcy07121314 天前
Python中使用FastAPI和HDFS进行异步文件上传
python·fastapi