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"}
相关推荐
zizle_lin7 小时前
优雅使用Gunicorn进程管理FastAPI
服务器·fastapi·gunicorn
掘金-我是哪吒2 天前
分布式微服务系统架构第131集:fastapi-python
分布式·python·微服务·系统架构·fastapi
Anesthesia丶4 天前
Vue3 + naive-ui + fastapi使用心得
vue.js·ui·fastapi
weixin_307779134 天前
使用FastAPI微服务在AWS EKS中构建上下文增强型AI问答系统
人工智能·python·云计算·fastapi·aws
掘金-我是哪吒5 天前
分布式微服务系统架构第130集:Python工程化FastAPI,运维Nginx-keepalived+Nginx实现高可用集群
运维·分布式·微服务·系统架构·fastapi
LingRannn5 天前
JWT的介绍与在Fastapi框架中的应用
fastapi
Python私教5 天前
使用FastAPI和React以及MongoDB构建全栈Web应用05 FastAPI快速入门
前端·react.js·fastapi
Python私教6 天前
全栈开发实战:FastAPI + React + MongoDB 构建现代Web应用
前端·react.js·fastapi
weixin_307779136 天前
使用FastAPI和Apache Flink构建跨环境数据管道
redis·python·云计算·fastapi·aws
真智AI9 天前
构建安全的机器学习推理API:基于FastAPI的用户认证与管理实战
安全·机器学习·fastapi