fastapi
fastapi copied to clipboard
List as query param
Here I'm creating operation , and I want to pass to it multiple query parameters like
/test?query=1&query=2&query=3
this is my code:
from typing import List
from fastapi import FastAPI, Query, Body
app = FastAPI(debug=True)
@app.get("/test")
def check(query: List[int]):
return query
Expected behavior
in docs I should see this param if I call it should accept GET params
Screenshots
No parameters in docs:
when I try to call it - got an error that body is not supplied:
Environment
- OS:macOS
- FastAPI 0.54.1
- Python 3.7.4
This works:
from typing import List
from fastapi import FastAPI, Query
app = FastAPI()
@app.get("/test")
async def test(query: List[int] = Query(...)) -> List[int]:
return query
All the best :)
Your request might have been mapped to https://fastapi.tiangolo.com/tutorial/body/. Unfortunately I am not sure if your use case is supported at all. Probably by reading request: Request
directly? You can try reading Request document in Starlette to figure it out.
Alright https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#query-parameter-list-multiple-values
You can close the issue if it works for you.
This works: async def test(query: List[int] = Query(...)) -> List[int]: All the best :)
Yes, it works, but my guess is - either FastAPI should raise some validation error or it should treat default params as Query (like it does for string, int annotations)
I think the error points to missing query
in the body of the request. That is what I understood :)
Thanks for the help here everyone! :clap: :bow:
@vitalik you are using a "complex" data type, a list of ints, by default it will try to read it from the body. You have to make it explicit that you want it from a query if you want it to come from the query.
yeah, I would say either it should allow it in get or it should make schema docs for request body
OR it should validate - that user must not declare body param that is not supported by http method (GET)
Actually, FastAPI supports reading bodies from GET
requests. It's not standard, it's not part of the specs, but it's allowed for compatibility, for example with ElasitcSearch.
Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.