Fastapi: [BUG] List as query param

Created on 13 May 2020  路  7Comments  路  Source: tiangolo/fastapi

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:

CleanShot 2020-05-13 at 18 14 02

when I try to call it - got an error that body is not supplied:

CleanShot 2020-05-13 at 18 15 50

Environment

  • OS:macOS
  • FastAPI 0.54.1
  • Python 3.7.4
question

Most helpful comment

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 :)

All 7 comments

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)

Was this page helpful?
0 / 5 - 0 ratings