2

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 "[email protected]" 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 
1

1 Answer 1

3

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)}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks . I'm using curl for testing . I assume , it will be the same , if i call this function from some other python program/script. one follow up quesion, in the event , where i have to send some more data , like other function {'someotherdata' : 'moredata' }, along with the file , to the same function . how should i send/receive that data
When you're sending a file and some additional form data to a FastAPI endpoint — whether using curl or another Python script (like requests) — you can handle this using a multipart/form-data request.
curl -X POST "http://127.0.0.1:8000/upload_file_with_data/" \ -F "[email protected]" \ -F "someotherdata=moredata" -F is used to send form fields in multipart/form-data format, both for the file and the additional fields.
Thanks again @ajeet. Is there a way , I can define my method such that I can upload a file, and at the same time send additional key/value pair data , that maps to my data class as well . How would that function look like
Please take a look at this answer on how to upload both file(s) and key/value pairs at the same time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.