Skip to content

FastAPI does not allow to send a "None" UploadFile #12039

@phuchm

Description

@phuchm

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

I have a POST method with FastAPI which has 2 params with UploadFile type. But one of them is required and one of them is Optional. When I send required param with None value, I got 422 error relating to "Field required". But if I send Optional param with None value, it's acceptable.
When I use:
from fastapi import Request
And add:
request: Request as extra param, then log request.form(), I don't see Optional param.
It seems to FastAPI ignored it?

My code is below:
from fastapi import File, Request, UploadFile
from typing import Annatoted

app: FastAPI = FastAPI()

@app.post(/test)
async def test(
request: Request,
firstFile: Annatoted[UploadFile, File(...)],
secondFile: Annatoted[UploadFile | None, File(...)] = None):
logger.debug(await request.form())

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions