Reputation: 487
Pydantic
model looks like ths:class Banner:
title: str
text: str
@router.post('', status_code=201)
async def create_banner(
banner: Banner,
photo: UploadFile = File(...) # multipart/form-data
):
return await Banners.create(banner.dict())
Upvotes: 7
Views: 27319
Reputation: 34700
Please have a look at this answer for more options on how to upload a File
together with JSON
data.
In short, you can't have Pydantic models (JSON
data) defined together with Form
(and/or File
) data. You can either use Form
fields, i.e, sending the data as form-data
in the body:
@router.post("/")
def create_banner(title: str = Form(...), text: str = Form(...), photo: UploadFile = File(...)):
return {"JSON Payload ": {"title": title, "text": text}, "Uploaded Filename": photo.filename}
or, use Dependencies with Pydantic models, i.e., sending the data as query
parameters:
from pydantic import BaseModel
from fastapi import Depends
class Banner(BaseModel):
title: str
text: str
@router.post("/")
def create_banner(banner: Banner = Depends(), photo: UploadFile = File(...)):
return {"JSON Payload ": banner.dict(), "Uploaded Filename": photo.filename}
Upvotes: 12
Reputation: 20569
According to the FastAPI docs:
You can declare multiple
File
andForm
parameters in a path operation, but you can't also declareBody
fields that you expect to receive as JSON, as the request will have the body encoded usingmultipart/form-data
instead ofapplication/json
.This is not a limitation of FastAPI, it's part of the HTTP protocol.
And when passing an object, FastAPI will try to treat it as a body specification, not as a form field. That means, you have to explicitly define your banner
argument as a form field:
@router.post('', status_code=201)
async def create_banner(
banner: Banner = Form(...),
photo: UploadFile = File(...) # multipart/form-data
):
return await Banners.create(banner.dict())
Make also sure that your Banner
object is a valid pydantic model, as FastAPI can't recognize bare objects properly in this context.
Upvotes: 3