I would highly suggest you send the data as a request body (in JSON
form) using a POST
request instead, as described in the documentation. You can create a Pydantic model (let's say Item
), as described in the above documentation, and then define your endpoint to expect a list of that Item
(as shown here). For the gender
, you could make use of the Python's Literal
type, which is supported by Pydantic and allows you specifying that a field may accept only specific literal values. Example is given below. You could test that through OpenAPI at http://127.0.0.1:8000/docs.
from pydantic import BaseModel
from typing import List
from typing import Literal
class Item(BaseModel):
gender: Literal['male', 'female']
age: int
income: int
spending: int
@app.post("/items")
def create_item(items: List[Item]):
return items
If you still, however, need to do that using a GET
request and query parameters (which I wouldn't suggest), you could define List
fields explicitly declared with Query
, so that gender
, age
, etc., can appear multiple times in the URL, and you would have to loop through all the parameters to get your instances (see here and here). Alternatively, you could use arbitrary query parameters, where you send the data in a form that suits you to get your instances. You can get the query params using the Request
object, i.e., request.url.query
or request.query_params
(see this answer for more details). However, in this way, your data won't be validated, and thus, you need to make sure you get the correct types and required parameters for your input data.