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"}
相关推荐
蓝倾1 天前
如何将淘宝获取店铺所有商品API接口应用到自己店铺?实战讲解详细步骤
api·fastapi
在星空下1 天前
Fastapi-Vue3-Admin
前端·python·fastapi
onelafite8 天前
一键式商品信息获取:京东API返回值深度挖掘
api·fastapi
苏侠客8529 天前
在docker上部署fastapi的相关操作
docker·容器·fastapi
令狐寻欢10 天前
AI 大模型应用进阶系列(五):FastAPI 入门
人工智能·python·fastapi
六毛的毛14 天前
FastAPI入门:中间件、CORS跨域资源共享、SQL数据库
数据库·中间件·fastapi
蓝倾14 天前
批量获取亚马逊商品SKU商品规格调用流程
api·fastapi
蓝倾17 天前
京东商品销量数据如何获取?API接口调用操作详解
前端·api·fastapi
蓝倾21 天前
小红书获取笔记详情API接口调用操作指南
前端·api·fastapi
六毛的毛21 天前
FastAPI入门:表单数据、表单模型、请求文件、请求表单与文件
前端·python·fastapi