当前位置:网站首页>关于如何向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)],
)
边栏推荐
猜你喜欢
随机推荐
动态调整web主题(2) 萃取篇
初识C语言
0.ROS常用命令
【函数与递归】7.19
Flask,7
VSO Downloader Ultimate 5.0.1.45 中文多语免费版 在线视频下载工具
MySQL EXPLAIN 性能分析工具详解
2.ROS通信机制
ss-2.子项目互相访问(order80 -> payment8001)
breed Web刷机升级详细教材修正编译器固件说明_itkeji.top
docker mysql 容器中执行mysql脚本文件并解决乱码
《录取通知》 观后感
三角形个数
Redis6学习笔记
取某一区间中素数的个数--洛谷P1865 A % B Problem
生活原则。
Django从入门到放弃三 -- cookie,session,cbv加装饰器,ajax,django中间件,redis缓存等
Navicat 解决隔一段时间不操作出现延时卡顿问题
Pr第三次培训笔记
二叉树的合并[C]









