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"}
相关推荐
风早爽太2 天前
Python 开发笔记:配置生产环境中 Celery Worker 的独立进程启动方式
python·fastapi
Java的搬运工4 天前
【无标题】
python·机器学习·docker·fastapi·模型部署·mlops·ai工程化
云和数据.ChenGuang4 天前
langchain4j的RAG入门
人工智能·深度学习·机器学习·语言模型·fastapi
风早爽太4 天前
用 ‌Render‌ 快速部署网站和常见问题解决
python·fastapi
艺杯羹5 天前
240GB流量击穿与频繁OOM:从云端瘫痪到客户端自治的本地优先架构重构实录
性能优化·系统架构·fastapi·桌面客户端·本地优先
创新技术阁5 天前
FastapiAdmin代码生成详细解释
前端·后端·fastapi
像风一样自由20205 天前
41.用FastAPI搭建一个RAG后端需要哪些接口
人工智能·大模型·fastapi·rag·智能体
databook6 天前
手把手带你走一遍:机器学习模型如何用FastAPI和Docker部署
python·docker·fastapi
EatFans6 天前
Docker 实战部署:从本地镜像到云服务器,一篇走通 FastAPI + Celery + MySQL + Redis + Nginx
docker·fastapi
创新技术阁6 天前
FastapiAdmin插件介绍
前端·后端·fastapi