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"}
相关推荐
摸鱼仙人~3 天前
ImportError: cannot import name ‘FixtureDef‘ from ‘pytest‘
conda·pytest·fastapi
安迪小宝4 天前
20 FastAPI 性能优化
oracle·性能优化·fastapi
_.Switch9 天前
FastAPI 高并发与性能优化
网络·数据库·python·性能优化·fastapi
安迪小宝12 天前
11 FastAPI文档自定义
fastapi
drebander12 天前
[特殊字符] 基于 FastAPI 和 React 构建车牌号识别网站
前端·react.js·fastapi
m0_7482329213 天前
纯 Python、Django、FastAPI、Flask、Pyramid、Jupyter、dbt 解析和差异分析
python·django·fastapi
一见已难忘15 天前
Flask与FastAPI对比选择最佳Python Web框架的指南
python·flask·fastapi
亿牛云爬虫专家17 天前
FastAPI与Selenium:打造高效的Web数据抓取服务
爬虫·python·selenium·fastapi·图片·代理ip·pixabay
股票数据接口19 天前
2025年度Python最新整理的免费股票数据API接口
开发语言·python·fastapi
老大白菜20 天前
在 Ubuntu 中使用 FastAPI 创建一个简单的 Web 应用程序
前端·ubuntu·fastapi