我正在try 将一些路由添加到FastAPI项目中.新的路由不是事先确定的,代码应该从文件中读取它们.要添加我使用的add_api_route
路由,如下所示:
from fastapi import APIRouter
my_router = APIRouter()
def foo(xyz):
return {"Result": xyz}
my_router.add_api_route('/foo/{xyz}', endpoint=foo)
上面的工作正常.
然而,Enrty路径参数不是固定的,我需要从文件中读取它们,为了实现这一点,我try 如下所示:
from fastapi import APIRouter
my_router = APIRouter()
def foo(**kwargs):
return {"Result": kwargs['xyz']}
read_from_file = '/foo/{xyz}' # Assume this is read from a file
my_router.add_api_route(read_from_file, endpoint=foo)
但它抛出了这个错误:
{"detail":[{"loc":["query","kwargs"],"msg":"field required","type":"value_error.missing"}]}
FastAPItry 在foo
签名中找到实际参数xyz
.
在FastAPI中有什么方法可以实现这一点吗?或者甚至有什么解决方案可以接受像/foo/... whatever .../
这样的路径?