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.

Query parameters - Dictionary support?

See original GitHub issue

Description

While am using this feature, I have this requirement where I need a parameter to be a Dictionary(key values list) for ‘GET’ query parameters. Currently the GET operation seem to support only the following parameter types

  • String
  • Int
  • Enum
  • List[str] or List[int]
  • Kwargs(variable number of args in dictionary form)

Do you have any recommendations?. One suggestion I got is to use POST instead of GET and achieve the flexible query. Is that a good idea?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
vitalikcommented, May 15, 2020

I guess you can use Depends to achieve it:

import json
from typing import List
from fastapi import FastAPI, Query, Depends


app = FastAPI(debug=True)


def location_dict(locations: List[str] = Query(...)):
    return list(map(json.loads, locations))


@app.get("/test")
def operation(locations: list = Depends(location_dict)):
    return locations

CleanShot 2020-05-15 at 13 00 21

3reactions
ffss92commented, Mar 27, 2021

Looking at starlette documentation today, I found that you can get a dict of query params by using request like this

`from fastapi import FastAPI, Request from pydantic import BaseModel from typing import Optional from datetime import date

app = FastAPI()

class Query(BaseModel): msg: Optional[str] = None page: Optional[int] = None only_one: bool when: date

@app.get(‘/’) async def home(req: Request, only_one: bool, when: date, msg: str = None, page: int = None): query = Query(**req.query_params).dict(exclude_unset=True) return query`

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting Query Parameters as Dictionary in FastAPI
One of the requirements for my application is to get all the query parameters in a dictionary (there are multiple combinations, ...
Read more >
Serialised dictionary in GET query parameters
Dictionaries sent in the query string are usually serialized as "style: form" + "explode: true". See this link for details and examples:.
Read more >
Can I use dictionary to add my query parameters in restsharp ...
I have used a Dictionary<string, string> for my query params but I am getting an error. It shows AddQueryParameter takes string name, ...
Read more >
passing dictionary in URL not working. I Get a string
request.vars is simply a representation of a URL query string, which is a collection of key=value items, where key and value are both...
Read more >
Query Parameters - FastAPI
When you declare other function parameters that are not part of the path parameters, they are automatically interpreted as "query" parameters. from fastapi ......
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 Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found