Skip to content

How to define empty query parameters as missing? #1147

@levchik

Description

@levchik

First 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}

GET http://URL/test/?q=

or

GET http://URL/test/?q

results in:

{
  "detail": [
    {
      "loc": [
        "query",
        "q"
      ],
      "msg": "value is not a valid integer",
      "type": "type_error.integer"
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions