-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Description
Opening a new issue since no action has been taken on #842 for more than a month.
Looks like we have an issue on how fastapi handles lists of elements passed as Form parameter (so anything that is type hinted List[...] = Form(...)). The issue doesn't arise when they are defined as Query parameters.
After some digging I managed to get to the root of the problem, which is how we pass the parameters when making an api call. fastapi indeed correctly understands that the input needs to be a list of elements, but doesn't parse the list if it comes from a single field.
Example using Query, works as expected
@app.post("/")
def api_test(l: List[int] = Query(...)) -> List[int]:
return lApi request generated using the swagger UI
curl -X 'GET' \
'http://localhost:5009/?l=1&l=2' \
-H 'accept: application/json'note how the passed values are l=1&l=2
[
1,
2
]Example using Form, doesn't work as expected
@app.post("/")
def api_test(l: List[int] = Form(...)) -> List[int]:
return lApi request generated using the swagger UI
curl -X 'POST' \
'http://localhost:5009/' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'l=1,2'note how the passed values are l=1,2
{
"detail": [
{
"loc": [
"body",
"l",
0
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}Manual api request
curl -X 'POST' \
'http://localhost:5009/' \
-H 'accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'l=1' \
-d 'l=2'Now the values are passed as -d 'l=1' and -d 'l=2'
[
1,
2
]I am fairly sure that l=1,2 should be an accepted way of passing a list of values as parameter, but fastapi doesn't seem to like it. Also, if this isn't the case, the Swagger UI doesn't produce the correct curl requests for lists given as Form parameters.
Packages:
fastapi:0.66.0python:3.7.4
Let me know if you need other details!
Thank you for coming to my TED talk