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"}
相关推荐
Python私教1 天前
FastAPI+React19 ERP系统实战 第02期
fastapi
Python私教1 天前
FastAPI+React19 ERP系统实战 第01期
react·fastapi
小宁爱Python4 天前
FastAPI+Sqlite+HTML的登录注册与文件上传系统:完整实现指南
sqlite·html·fastapi
fairymt4 天前
LoRA 问答微调与部署全流程:基于 LLaMA-Factory + DeepSeek + FastAPI 打造专属大模型
fastapi
chenkangck5013 天前
Uvicorn 入门详解
python·fastapi
remandancy.h17 天前
FastAPI:(2)开启FastAPI
fastapi
里探17 天前
FastAPI的初步学习(Django用户过来的)
python·django·fastapi
从零开始学习人工智能17 天前
基于FastAPI与Selenium的智能开关状态管理系统实践
selenium·adb·fastapi
Python私教19 天前
FastAPI本地文档的定制技巧
fastapi
沛沛老爹20 天前
Celery+fastAPI/Flask实现高性能应用
python·flask·fastapi·celery·web框架集成