【发布时间】:2022-11-10 02:05:34
【问题描述】:
我的问题是,我怎样才能获得路径参数的默认值才能工作?
对于以下端点:
from FastAPI import Path as fPath
@app.get("/users/{code}")
async def get_user(code: str = fPath("hellomotto", regex=r'hello.*')):
return {"code": code}
现在,如果我访问localhost:666/users/helloworld,它会给我一个很好的回应:
{"code": "helloworld"}
然而,如果我尝试去localhost:666/users/,它会给我一个{ "detail": "Not Found" }的回复
有没有办法让它默认返回{"code": "hellomotto"},以防用户没有输入localhost:666/users/hellomotto之类的东西
当然,我可以只为/users/ 设置一个端点,但我想我可以设置一个默认值...
编辑:也试过default="hellomoto"
edit2:当我尝试使用查询参数时,它确实采用了默认值......
【问题讨论】: