我想创建一个 FastAPI API 端点,它依赖于 HTTP GET 参数,有文档说明,并使用 FastAPI 的验证功能。考虑以下最简示例:
import fastapi
app = fastapi.FastAPI(
)
@app.get("/endpoint")
def example_endpoint(
par1: int = fastapi.Query(
None,
description="example documentation1",
),
par2: int = fastapi.Query(
None,
description="example documentation2",
),
):
return {"test": par1 + par2}
它具有文档支持并通过HTTP GET参数工作,但不验证它们 - http://localhost:8000/endpoint?par1=2&par2=3正常工作,但是http://localhost:8000/endpoint崩溃并显示内部服务器错误,而不是通知用户需要一个参数。有没有办法使par1和par2为必需,并保留文档功能?
Query(...)
中的example=1
是什么意思? - JPG/endpoint
时,您仍将收到一个错误{"detail":[{"loc":["query","par1"],"msg":"field required","type":"value_error.missing"}
。 - Yagiz Degirmenci