question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] How to pass list in query parameters?

See original GitHub issue

Description

I need to support several query parameters with same name in GET route. Typical request looks like this: http://localhost/item?num=1&num=2

I configured a route

@app.get("/item", content_type=UJSONResponse)
async def get_part(num: list):
    found = False
    for i in num:
         if i in some_data:
             found = True
             break
    return {"found": found}

The idea is, that I can pass several numbers to check, if they exist in some_data. In my opinion, it is not a good idea to get numbers from body here, because it is a simple GET request, but in current version application expects argument “num” in body, so on my request (written above) I receive a response

{"detail":[{"loc":["body","num"],"msg":"field required","type":"value_error.missing"}]}

As I know, it is not restricted to pass several query parameters with the same name in an HTTP request by any specifications, so I would like to ask, is it possible to configure a route in FastAPI, which will be able to parse several query parameters with the same name?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

83reactions
tiangolocommented, Feb 23, 2019

It is supported (and tested), but not documented yet, but you can just use standard Python types (a List) 😃

from typing import List

from fastapi import FastAPI, Query

app = FastAPI()
@app.get("/items/")
def read_items(q: List[int] = Query(None)):
    return {"q": q}
38reactions
neilpanchalcommented, Mar 6, 2020

Is it possible to do: http://localhost/item?num=1,2,3,4,5,6

instead of: http://localhost/item?num=1&num=2&num=3&num=4&num=5&num=6?

Read more comments on GitHub >

github_iconTop Results From Across the Web

REST API Best practice: How to accept list of parameter ...
The standard way to pass a list of values as URL parameters is to repeat them: http://our.api.com/Product?id=101404&id=7267261.
Read more >
Spring — Passing list and array of values as URL parameters
Let's see how to send a list of query parameters, and receive them in array/arraylist in Spring. Sending via URL: List of values...
Read more >
Query Parameters and String Validations - FastAPI
To declare a query parameter with a type of list , like in the example above, you need to explicitly use Query ,...
Read more >
Send Array List through Query Parameters on HTTP Request ...
But i would like to pass them as list through query string params using IN operator in database. select Query: SELECT * from...
Read more >
Passing Information via Query Strings - Qualtrics
The second query string begins with an ampersand (&) instead of a question mark (?). This is because URLs can only support one...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found