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 天前
02. FastAPI——路由
服务器·前端·后端·python·pycharm·fastapi
在屏幕前出油2 天前
00. FastAPI——了解FastAPI
后端·python·pycharm·fastapi
夫唯不争,故无尤也2 天前
curl与Invoke-RestMethod核心区别
后端·fastapi·powershell·curl
qiuyuyiyang2 天前
开源模型应用落地-FastAPI-助力模型交互-进阶篇-中间件(四)
开源·交互·fastapi
安逸sgr2 天前
【端侧 AI 实战】BitNet 详解:1-bit LLM 推理优化从原理到部署!
人工智能·python·scrapy·fastapi·ai编程·claude
曲幽4 天前
🐢 从0到1,FastAPI + PostgreSQL + Tortoise ORM 实战避坑指南
postgresql·fastapi·orm·migration·pythonweb·asyncpg·tortoise·aerich
rising start4 天前
FastAPI进阶开发:中间件、依赖注入
中间件·fastapi·依赖注入