I am learning web development, I have some experience in python scripting. This time, I wanted to create an api in python, so review fast api docs. I have the following set up (example contrived for the purpose of this post). Based on the examples in fast api site, I created following python code, which has two methods. Also, I have defined classes that represent the data I want to read in these methods. My understanding is that pydantic library validates data. So, for testing, I used curl command as such, which works. Pydantic maps the data {'name': 'foo'}
I sent in curl command, to the class I have defined Item
. Is there any documentation on how it maps data? For my next method, I want to post contents of a file, like video/audio data, which I understand I can send as raw binary data. I have defined FileContent
class with a field that can store bytes. but when I try to post file contents,
curl -X POST -F "file=@somefile.wav" http://127.0.0.1:5000/upload_file_content
I get json serialization error. how can I map the binary content of the file, I send via curl command to the FileContent class?
curl -X "POST" \
"http://127.0.0.1:5000/items" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{\"name\": \"foo\"}"
from fastapi import FastAPI
from pydantic import BaseModel, bytes
app = FastAPI()
class FileContent(BaseModel):
data: bytes
class Item(BaseModel):
name: str
@app.post("/items/")
async def create_item(item: Item):
return item
@app.post("/upload_file_content/")
async def upload_file_content(file_content: FileContent):
# do something with file content here
You're on the right track with FastAPI and Pydantic, but binary file uploads via curl -F
(i.e., multipart/form-data
) don't get automatically mapped to a Pydantic model like FileContent
.
When dealing with file uploads, FastAPI provides a special way to handle binary data using UploadFile
, not bytes
directly in a Pydantic model.
Here’s how to rewrite your /upload_file_content/
endpoint:
from fastapi import FastAPI, File, UploadFile
from pydantic import BaseModel
app = FastAPI()
class Item(BaseModel):
name: str
@app.post("/items/")
async def create_item(item: Item):
return item
@app.post("/upload_file_content/")
async def upload_file_content(file: UploadFile = File(...)):
contents = await file.read() # Get file contents as bytes
return {"filename": file.filename, "size": len(contents)}