Skip to content

Nullable Query Parameters #4708

@jurasofish

Description

@jurasofish

First Check

  • I added a very descriptive title to this issue.
  • 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.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from typing import Optional

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
def home(q: Optional[bool] = True):
    return {"Hello": q}

import uvicorn
uvicorn.run(app, host="127.0.0.1", port=5001)

Description

I have a simple FastAPI route with an Optional[bool] query parameter.
I would like to pass None as the value to the parameter.

I've tried all the following but in every case it is rejected with the messager {"detail":[{"loc":["query","q"],"msg":"value could not be parsed to a boolean","type":"type_error.bool"}]}

  • GET /q=
  • GET /q
  • GET /q=null
  • GET /q=none

Is there any way to pass None other that setting it as the default value?

Operating System

macOS

Operating System Details

No response

FastAPI Version

0.73.0

Python Version

Python 3.9.7

Additional Context

No response

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