1

This is my FastAPI Code:

@app.get("/")
    async def read_index(
        zones: Optional[List[str]] = None
        ):
    print("zones:", zones)
        for house in load:
             if zones and house["location"] in zones:
                    continue

I am trying to filter a list of houses in "load". My problem is that I need to filter multiple str of zones, and it needs to accept either none, a string or a list.

I have been trying to debug for a while, by instering into the url ?zones=London or London, Cambridge, to see it was a problem with the list.

Until I tried to put the print statement and tested it with either list or string, which returned.

zonas: None

Which means that the url is not even accepting the query? So I am not sure how to even pass this parameter as a list?

  • 1
    Hello! Does this help you? [https://github.com/tiangolo/fastapi/issues/50#issuecomment-466700096](https://github.com/tiangolo/fastapi/issues/50#issuecomment-466700096) Looks like a discussion in the official repo about the same (or very similar) problem! – Cuartero Mar 28 '23 at 16:06
  • 1
    There are some options here https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#query-parameter-list-multiple-values – Henrique Andrade Mar 28 '23 at 16:12
  • Related answers that you might find helpful can be found [here](https://stackoverflow.com/a/73982479/17865804), as well as [here](https://stackoverflow.com/a/70845425/17865804) and [here](https://stackoverflow.com/a/70840850/17865804) – Chris Mar 28 '23 at 16:41

1 Answers1

1

It seems the solution was this:

zones: Optional[List[str]] = Query(default=None)