[BUG] Allow json/BaseModels in query parameters
See original GitHub issueIs your feature request related to a problem
I have to support getting a structured json string as parameter:
https://localhost:8080/test?moop={"name": "something %26 something else","description": "str" ,"price": 0.2,"tax": 0.2}&foo=bar
I would like to have a easy validation for those fields as well, as this could be automatic
The solution you would like
@routes.api_route('/test', methods=['GET', 'POST'], tags=['official', 'message', 'send'])
async def test(
moop: Json[Item] = Query(..., description='Something something'),
foo: str = Query(...),
):
return {'woop': moop, 'lol': foo}
# end def
Initial version:
This was the initial thought, before I discovered the Json
type.
@routes.api_route('/test', methods=['GET', 'POST'], tags=['official', 'message', 'send'])
async def test(
moop: Item = Query(..., description='Something something'),
foo: str = Query(...),
):
return {'woop': moop, 'lol': foo}
# end def
Bug you face
AssertionError: Param: moop can only be a request body, using Body(...)
Describe alternatives you’ve considered
moop: str
Issue Analytics
- State:
- Created 4 years ago
- Reactions:5
- Comments:24 (4 by maintainers)
Top Results From Across the Web
Plus sign in query string - Stack Overflow
In the above examples, Google's server script is URL-decoding the query parameters and then using them to do the search. URL-encoding is nothing...
Read more >Using Query Parameters (AWS Signature Version 4)
Using query parameters to authenticate requests is useful when you want to express a request entirely in a URL. This method is also...
Read more >Use parameters to ask for input when running a query
To specify the data type for parameters in a query: With the query open in Design view, on the Design tab, in the...
Read more >Running parameterized queries | BigQuery - Google Cloud
the bq command-line tool's bq query command; the API; the client libraries. The following example shows how to pass parameter values to a...
Read more >Query Parameters and String Validations - FastAPI
FastAPI allows you to declare additional information and validation for your parameters. Let's take this application as example: Python 3.6 and above Python ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’d like to ask again if this issue can be re-opened. I’m currently implementing a analytics backend which requires some rather complex query parameters in GET requests. I really enjoy writing models and schemas once and use them all out the code to just relax and watch fastapi to build the API documentation for me. It’s a real bummer that dirty hacks and dependency-work arounds (including me writing OpenAPI spec manually) are necessary to get this to work for every new function I implement.
I think I found a good enough generic solution for parsing models in query strings.
Usage example:
The code of
json_param()
is available in the gist snippet by the link.https://gist.github.com/imankulov/cef71dd5a01f9a27caeb66f7bedaf241