当前位置:网站首页>关于如何向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)],
)
边栏推荐
猜你喜欢
随机推荐
icebreaker的垃圾话学习指南
flask 面试题 问题
VSO Downloader Ultimate 5.0.1.45 中文多语免费版 在线视频下载工具
机器码介绍
JS学习笔记(三)
令人愉快的 Nuxt3 教程 (二): 快速轻松地搭建博客
pta a.1003 的收获
Junit
初识C语言
-整数求和-
飞机大战完整版
Kaggle 入门(Kaggle网站使用及项目复现)
初步认识ZK
OptionError: ‘Pattern matched multiple keys‘
《录取通知》 观后感
Go (一) 基础部分2 -- if条件判断,for循环语句
一维数组和二维数组的命名以及存储空间
-查找数-
0.ROS常用命令
三角形个数