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 天前
基于FastAPI与LangChain的Excel智能数据分析API开发实践
langchain·excel·fastapi
杰瑞学AI5 天前
我的全栈学习之旅:FastAPI (持续更新!!!)
后端·python·websocket·学习·http·restful·fastapi
Python私教8 天前
浏览器不再拦请求:FastAPI 跨域(CORS)配置全解析
fastapi
带娃的IT创业者10 天前
第2集:技术选型的智慧:Flask vs FastAPI,GLM-4 vs GPT
python·gpt·flask·fastapi·glm·技术选型
万粉变现经纪人14 天前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
闲人编程15 天前
2025年,如何选择Python Web框架:Django, Flask还是FastAPI?
前端·后端·python·django·flask·fastapi·web
chao_78915 天前
Union 和 Optional 区别
开发语言·数据结构·python·fastapi
BTU_YC16 天前
Django+FastAPI+Vue微服务架构指南
架构·django·fastapi
Gerlat小智16 天前
【Python精讲 16】实战项目演练(二):用Flask/FastAPI发布你的第一个Web API
python·flask·fastapi
onelafite17 天前
淘宝开放平台拍立淘接口返回参数及调用操作指南
api·fastapi