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"}
相关推荐
深海鱼肝油ya18 小时前
基于FastAPI的AI智能体Web系统构建(二)
人工智能·fastapi·python开发·异步框架·agent开发
咕噜咕噜啦啦2 天前
FastAPI
fastapi
CaffeinePro4 天前
FastAPI高并发实战:缓存、分布式限流、链路日志三位一体架构
后端·fastapi
java1234_小锋4 天前
FastAPI python web开发- 集成SQLAlchemy ORM 操作数据库
fastapi
不瘦80斤不改名5 天前
全家桶、乐高积木与类型引擎:重新认识 Django、Flask 与 FastAPI
django·flask·fastapi
Exclusive_Cat6 天前
FastAPI热重载失效?降级uvicorn解决
python·fastapi
初学AI的小高7 天前
FastAPI 工程实践:LLM 应用的 API 设计之道
后端·fastapi
java1234_小锋7 天前
FastAPI python web开发- 中间件
fastapi
不能只会打代码7 天前
Day 002 — Python 工程化 & FastAPI & 数据库速通
数据库·redis·python·docker·fastapi·测试
zhiSiBuYu05177 天前
FastAPI 结合 Jinja2 模板开发入门指南
fastapi