[QUESTION] How to define empty query parameters as missing?
See original GitHub issueFirst check
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
Description
How can I make empty query parameters to be interpreted as missing value (and therefore default value is used) instead of empty string? For example, if I were to use optional int query param, empty string would find its way as value being passed and validation error for invalid int would be raised.
Additional context
Sample code:
from fastapi import APIRouter
router = APIRouter()
@router.get("/test/", tags=["test"])
def test(q: int = None):
return {"q": q}
or
results in:
{
"detail": [
{
"loc": [
"query",
"q"
],
"msg": "value is not a valid integer",
"type": "type_error.integer"
}
]
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Checking for null and missing query string parameters in ...
I want to be able to distinguish between existing query string parameters set to null, and missing parameters. So the parts of the...
Read more >How To Send A Request With Empty Query Parameters?
How To Send A Request With Empty Query Parameters? To mark a parameter as required, you need to go to the Request editor...
Read more >Solved: How to raise a fault on missing or empty request p...
Hi,. I have an endpoint which needs an 'email' parameter and if the email parameter is missing or empty it need to raise...
Read more >How to overcome missing query parameters in Databricks ...
I'm trying to build up my first dashboard based on Dataabricks SQL. As far as I can see if you define a query...
Read more >Query string query | Elasticsearch Guide [8.5]
The query string “mini-language” is used by the Query string and by the q query ... and would not match if the field...
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
Same requirements here, and handled with a middleware at last.
We can just drop those fields with blank values, which are empty strings or strings contain only whitespaces, default values will be handled by
Query
models.However, as @tiangolo commented, this approach may not be correct, maybe it’s not recommended. 😃
Thanks for the answer. I met this issue when I use amis with FastAPI.
According to above code snippet, I make a litte optimization to simplify code.