当前位置:网站首页>关于如何向FastAPI的依赖函数添加参数
关于如何向FastAPI的依赖函数添加参数
2022-08-03 05:17:00 【小兜全糖(Cx)】
- 通过path 或者query添加参数
from fastapi import Depends, FastAPI
app = FastAPI()
async def my_dependency_function(item_id: int):
return {
"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(item_id: int, my_dependency: dict = Depends(my_dependency_function):
return my_dependency
- 显示的说明参数来源
from fastapi import Depends, FastAPI, Path
app = FastAPI()
async def my_dependency_function(item_id: int = Path(...)):
return {
"item_id": item_id}
@app.get("/items/{item_id}")
async def read_item(my_dependency: dict = Depends(my_dependency_function):
return my_dependency
- 将request对象作为参数
async def test_parameters(request:Request):
vv = request
return {
"result":"success"}
@app.get('/ellis/parameters')
async def ellis(token:str=Depends(verify_jwt),value:dict=Depends(test_parameters)):
return value
- 在路由层面设置dependends
items_router = APIRouter(
prefix="/items",
tags=["items"],
dependencies=[Depends(my_dependency_function)],
)
边栏推荐
猜你喜欢
随机推荐
运行 npm run xxx 如何触发构建命令以及启动Node服务等功能?
mysql 存储过程 动态参数 查询执行结果
【Nmap与Metasploit常用命令】
-整数求和-
Flask,3-6
令人愉快的 Nuxt3 教程 (二): 快速轻松地搭建博客
【DC-5靶场渗透】
Haproxy服务监控
Djiango第四次培训笔记
【扫雷】多方法超详细 7.28
第三次HarmonyOS培训
动态规划笔记
一劳永逸解决vs编译器无法使用scanf函数
【三子棋】7.25
【XSS,文件上传,文件包含】
动态调整web主题(2) 萃取篇
1.ROS环境搭建与基础工作
Junit
pta a.1003 的收获
7.24[C语言零基础 知识点总结]