fastapi icon indicating copy to clipboard operation
fastapi copied to clipboard

List as query param

Open vitalik opened this issue 4 years ago • 8 comments

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

vitalik avatar May 13 '20 15:05 vitalik

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

MichalMazurek avatar May 13 '20 15:05 MichalMazurek

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.

phy25 avatar May 13 '20 15:05 phy25

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.

phy25 avatar May 13 '20 15:05 phy25

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)

vitalik avatar May 13 '20 15:05 vitalik

I think the error points to missing query in the body of the request. That is what I understood :)

MichalMazurek avatar May 13 '20 15:05 MichalMazurek

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.

tiangolo avatar Jun 06 '20 13:06 tiangolo

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)

vitalik avatar Jun 08 '20 07:06 vitalik

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.

tiangolo avatar Nov 05 '22 13:11 tiangolo

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.

github-actions[bot] avatar Nov 16 '22 00:11 github-actions[bot]