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