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 天前
小红书获取关键词列表API接口详解
前端·后端·fastapi
蓝倾2 天前
淘宝获取商品规格接口(item-sku)操作详解
前端·后端·fastapi
蓝倾5 天前
电商开放平台API开发最佳实践:以淘宝开放平台为例
api·fastapi
FE杂志社5 天前
全栈开发 → FastAPI碎碎念
后端·python·fastapi
Json____10 天前
使用python的 FastApi框架开发图书管理系统-前后端分离项目分享
开发语言·python·fastapi·图书管理系统·图书·项目练习
Python私教13 天前
FastAPI+React19 ERP系统实战 第02期
fastapi
Python私教13 天前
FastAPI+React19 ERP系统实战 第01期
react·fastapi
小宁爱Python15 天前
FastAPI+Sqlite+HTML的登录注册与文件上传系统:完整实现指南
sqlite·html·fastapi
fairymt15 天前
LoRA 问答微调与部署全流程:基于 LLaMA-Factory + DeepSeek + FastAPI 打造专属大模型
fastapi
chenkangck5025 天前
Uvicorn 入门详解
python·fastapi