I have a FastAPI endpoint:
@router.post("/")
def create_vacation(
request: fastapi.Request,
paid: bool = False,
) -> None:
print(request.url, paid)
In the swagger, the url showed is:
http://127.0.0.1:880/vacation/?paid=true
and the cURL cmd:
curl -X 'POST'
'http://127.0.0.1:880/vacation/?paid=true'
-H 'accept: application/json'
-d ''
which result in:
http://127.0.0.1:880/vacation/?paid=true True
But when using the swagger UI, the print(request.url, paid)
shows:
http://127.0.0.1:880/vacation/ False
I use FastAPI for years, and this is still a first for me. Any clue?
I use the latest version of fastapi and pydantic (0.113.0
and 2.9.0
).
Here is a minimal reproducible example:
from fastapi import FastAPI
app = FastAPI(
title="Text",
)
@app.post("/")
def create_vacation(
paid: bool,
):
print(paid)
Then using swagger UI and selecting true
or false
in the dropdown:
We get this response:
{
"detail": [
{
"type": "missing",
"loc": [
"query",
"paid"
],
"msg": "Field required",
"input": null
}
]
}
7
It looks like the request did not recognize the query parameter ?paid
.
Did you see in Swagger under the parameter the (query)
You can try using Annotated
with Query
it might help resolve the issue
from fastapi import Request, Query
from typing import Annotated
@router.post("/")
def create_vacation(
request: Request,
paid: Annotated[bool, Query()] = False,
) -> None:
print(request.url, paid)
1