Skip to content
Discussion options

You must be logged in to vote

Declaring file upload fields using Pydantic model is not officially supported (see comment to closed PR).

To make your example work, you need to add File() annotation to file field:

class UploadForm(BaseModel):
    name: str
    file: Annotated[bytes, File()]

And specify media_type="application/x-www-form-urlencoded" as follows to make schema correct:

@app.post("?url=https%3A%2F%2Fgithub.com%2Fupload%2F")
async def upload(data: Annotated[UploadForm, Form(media_type="application/x-www-form-urlencoded")]):
    return {"name": data.name, "size": len(data.file)}

One more time: this is not officially supported. You can use it on your own risk)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@anushkapunekar
Comment options

Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem
2 participants