摘要:
请求体映射 from typing import Union from fastapi import FastAPI from pydantic import BaseModel app = FastAPI() class Item(BaseModel): name: str price: floa 阅读全文
posted @ 2024-02-28 17:22
我在路上回头看
阅读(42)
评论(0)
推荐(0)
摘要:
设置默认值 @app.get("/items/") async def read_items(q: Union[str, None] = Query(default=None, max_length=50)): # Query适用于查询参数中,设置默认值为None,并且规定最大长度为50 resul 阅读全文
posted @ 2024-02-28 17:17
我在路上回头看
阅读(60)
评论(0)
推荐(0)
摘要:
单个查询字符串 @app.get('/index/{username}') def index(username: str, id: int): # id为查询字符串 ?id=5 return {"message": "success", "username": username, "id": id 阅读全文
posted @ 2024-02-28 17:15
我在路上回头看
阅读(40)
评论(0)
推荐(0)
摘要:
路径参数额外校验Path from fastapi import Path app = FastAPI() @app.get('/items/{item_id}') async def read_items(item_id: str = Path(default=None, max_length=3 阅读全文
posted @ 2024-02-28 17:12
我在路上回头看
阅读(55)
评论(0)
推荐(0)
摘要:
路径参数 # 单一参数 @app.get('/index/{id}') def index(id: int): return {"message": "success", "data": id} # 多参数 @app.get('/index/{username}/{id}') def index(u 阅读全文
posted @ 2024-02-28 16:55
我在路上回头看
阅读(23)
评论(0)
推荐(0)
摘要:
mount应用挂载 1.创建主app应用对象实例,注册所属的路由信息 from fastapi import FastAPI from fastapi.response import JSONResponse app = FastAPI(title='主应用', description='主应用描述 阅读全文
posted @ 2024-02-28 16:54
我在路上回头看
阅读(512)
评论(0)
推荐(0)
摘要:
APIRouter实例的路由注册 API端点路由注册大致分为3种: 1.基于app实例对象提供的装饰器或函数进行注册 2.基于FastAPI提供的APIRouter类的实例对象提供的装饰器或函数进行注册 3.通过直接实例化APIRoute对象且添加的方式进行注册 路由注册方式 基于APIRouter 阅读全文
posted @ 2024-02-28 16:52
我在路上回头看
阅读(306)
评论(0)
推荐(0)
摘要:
APIRouter和 APIRoute的区别 # APIRouter 主要是定义路由组,可以理解为一个路由组的根路由,类似于flask的蓝图 # APIRoute 表示具体路由节点对象 阅读全文
posted @ 2024-02-28 16:51
我在路上回头看
阅读(146)
评论(0)
推荐(0)
摘要:
APIRouter参数介绍 class APIRouter(routing.Router): def __init__( self, *, prefix: str = "", # 表示当前路由分组的url前缀 tags: Optional[List[Union[str, Enum]]] = None 阅读全文
posted @ 2024-02-28 16:49
我在路上回头看
阅读(469)
评论(0)
推荐(0)
摘要:
一个路由配置多个http请求方法 app = FastAPI(routes=None) # 方式一:app.api_route() @app.api_route(path='/index', methods=['GET','POST']) async def index(): return {'ms 阅读全文
posted @ 2024-02-28 16:48
我在路上回头看
阅读(408)
评论(0)
推荐(0)