Fastapi: [QUESTION] q could be list type ?

Created on 7 Apr 2020  ·  12Comments  ·  Source: tiangolo/fastapi

hi,dear
How can I set the q be list type?
Is it possible ?

my request is json as down
{'data': [1,2,3,4,5]}

so how to set the read_item func ?
thx

question

Most helpful comment

@ucas010
The standard syntax for a query parameter that contains a list of values ​​is ?q=1&q=2&q=....
If you use requests or another client library to send requests, it will act as described above.

For example, this behavior using HTTPX:

import httpx

response = httpx.get("https://google.com/path", params={'q': [1, 2, 3, 4, 5]})

assert response.request.url == 'https://google.com/path?q=1&q=2&q=3&q=4&q=5'

FastAPI handles such requests out of the box. You can do it like this:

from typing import List

from fastapi import FastAPI

app = FastAPI()


@app.get("/path")
async def route_with_list_query(q: List[int]) -> None:
    print(q)

All 12 comments

Hi, @ucas010 !

I do not quite understand what you want to do. If I understand correctly, you need a model with a list type field, right?

I hope this example helps you. Also look here.

from typing import List
from pydantic import BaseModel

class Request(BaseModel):
    data: List[int]

...

@app.post('/url', response_model=List[int])
def foo(model: Request) -> List[int]:
    return model.data  # [1,2,3,4,5]

Maybe he is referring to the "q" parameter . Query in docs.

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

@Slyfoxy
thx
the problem is :
my client request is json ,the value is list
so how to code the script?
and the client script ?

thanks

client request data:
{'data':[1,2,3,4,5,6,7,8]}

my client request is json ,the value is list
so how to code the script?

Have you tried @Slyfoxy's code?

the client script

What do you mean by client script? What programming language? I don't think it's within fastapi scope at all. You can get some inspiration of the request by using the openapi docs.

@phy25
yes,I have tried with postman,
image

@ucas010 look at @jorgerpo example and this docs about using query params

And I think unfortunately if you want to parse the JSON in a query string, you have to use JSON in pydantic and do the heavy-lifting of validation yourself (you can definitely just use the parsed JSON object to create a pydantic model and do the validation during creation).

@ucas010
The standard syntax for a query parameter that contains a list of values ​​is ?q=1&q=2&q=....
If you use requests or another client library to send requests, it will act as described above.

For example, this behavior using HTTPX:

import httpx

response = httpx.get("https://google.com/path", params={'q': [1, 2, 3, 4, 5]})

assert response.request.url == 'https://google.com/path?q=1&q=2&q=3&q=4&q=5'

FastAPI handles such requests out of the box. You can do it like this:

from typing import List

from fastapi import FastAPI

app = FastAPI()


@app.get("/path")
async def route_with_list_query(q: List[int]) -> None:
    print(q)

Thanks for all the help here everyone! :bow: :clap:

thx,maybe flask is a little convenient.

I am not quite sure your point here though, in Flask you probably do json.loads(request.args) while in fastapi you do json.loads(q) if you defined q in the args.

Was this page helpful?
0 / 5 - 0 ratings