1

I send the list as param:

import requests
def test():
    params = {"test_param": [1, 2, 3]}
    response = requests.get("http://localhost:8000/test", params=params, timeout=20)
    response_data = response.json()
    print(response_data)

and handle everything with FastAPI

#!/usr/bin/env python3
from fastapi import FastAPI
from fastapi.responses import JSONResponse
import uvicorn

app = FastAPI()

@app.get("/test")
def test(test_param: list):
    return JSONResponse(content={"message": test})

if __name__ == "__main__":
    uvicorn.run("minimal:app", host="0.0.0.0", port=8000,
                workers=2)  # Use the on_starting event)

and I get the error "GET /test?test_param=1&test_param=2&test_param=3 HTTP/1.1" 422 Unprocessable Entity, but I thought that listing the parameter multiple times is the desired way of how FastAPI wants to receive lists...

My error might be that I am not using typing as described here, but imho that shouldn't influence the result as typing is ignored by python anyway: https://fastapi.tiangolo.com/tutorial/query-params-str-validations/#query-parameter-list-multiple-values

Natan
  • 728
  • 1
  • 7
  • 23
  • How to identify parameters if they all have the same name ? – Itération 122442 Aug 18 '23 at 08:34
  • It's a list so I assume that the first element has index 0 and the n-th index n-1. Afaik this is a standard. – Natan Aug 18 '23 at 08:44
  • Well that is strange. Even whe I copy/pasta the docs' sample, I don't have the same /docs display. Are you sure you have the fastapi version that matches the doc you are reading ? – Itération 122442 Aug 18 '23 at 09:00
  • 1
    Related answers to the one above can be found [here](https://stackoverflow.com/a/73982479/17865804), as well as [here](https://stackoverflow.com/a/70845425/17865804), [here](https://stackoverflow.com/a/73067473/17865804) and [here](https://stackoverflow.com/a/75998823/17865804) – Chris Aug 18 '23 at 09:00
  • 1
    As for returning a JSON response from the FastAPI backend, you might want to have a look at [this answer](https://stackoverflow.com/a/73974946/17865804) as well – Chris Aug 18 '23 at 09:04

0 Answers0