当前位置:网站首页>关于如何向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)],
)
边栏推荐
猜你喜欢
随机推荐
第三次HarmonyOS培训
Navicat 解决隔一段时间不操作出现延时卡顿问题
【转】最小描述长度准则MDL(Minimun Description Length)
动态调整web系统主题? 看这一篇就够了
【数组】arr,&arr,arr+1,&arr+1以及内存单元的占用
icebreaker的垃圾话学习指南
ansible的安装和部署详细过程,配置清单基本操作
docker mysql 容器中执行mysql脚本文件并解决乱码
小码农的第一篇博客
MySQL 优化建议详解
MySQL 索引检索原理和B+Tree数据结构详解
Redis6学习笔记
jsp通过form表单提交数据到servlet报404
第四次培训
Makefile介绍
【反弹shell与提权】
Sqli-labs-master靶场1-23关通关详细教程(基础篇)
`monorepo` 中 `hoist` 机制导致加载配置文件路径的变化
【打印菱形】
Pr第四次培训笔记


![7.24[C语言零基础 知识点总结]](/img/b8/3abcee495e70c9ffffc671f2b7d9b1.png)






