Consider this FastAPI endpoint that accepts a single file upload:
from fastapi import FastAPI, File, UploadFile
app = FastAPI()
@app.post('/upload')
async def upload_file(file: UploadFile = File(...)):
content = await file.read()
return {'filename': file.filename, 'size': len(content)}What will be the output if a file named example.txt with 100 bytes is uploaded?
from fastapi import FastAPI, File, UploadFile app = FastAPI() @app.post('/upload') async def upload_file(file: UploadFile = File(...)): content = await file.read() return {'filename': file.filename, 'size': len(content)}
Remember that file.filename gives the original file name and await file.read() reads the file content bytes.
The file.filename property contains the original uploaded file name. Using await file.read() reads the entire file content as bytes, so len(content) returns the file size in bytes. Thus, the returned JSON includes the correct filename and size.
Choose the correct way to declare a FastAPI POST endpoint that accepts a single file upload using UploadFile and File.
Remember that file uploads should be declared with UploadFile and File(...) to mark the parameter as required.
Option D correctly uses async def with file: UploadFile = File(...) to accept a required file upload asynchronously. Option D is missing async which is recommended for file reading. Option D uses bytes instead of UploadFile, so file.filename would fail. Option D misses = File(...) so the file parameter is not marked as required.
Given this FastAPI endpoint:
@app.post('/upload')
async def upload(file: UploadFile = File(...)):
content = file.read()
return {'size': len(content)}What error will occur when uploading a file and why?
@app.post('/upload') async def upload(file: UploadFile = File(...)): content = file.read() return {'size': len(content)}
Check if file.read() is awaited properly in async functions.
UploadFile.read() is an async method and must be awaited. Calling it without await returns a coroutine object, causing a RuntimeWarning and the file content is not read. This leads to unexpected behavior or errors.
Consider this FastAPI endpoint:
@app.post('/upload')
async def upload(file: UploadFile = File(...)):
file_content = await file.read()
await file.seek(0)
second_read = await file.read()
return {'first_read_size': len(file_content), 'second_read_size': len(second_read)}What will be the returned JSON if the uploaded file has 50 bytes?
@app.post('/upload') async def upload(file: UploadFile = File(...)): file_content = await file.read() await file.seek(0) second_read = await file.read() return {'first_read_size': len(file_content), 'second_read_size': len(second_read)}
Think about what seek(0) does to the file pointer before the second read.
The first await file.read() reads all 50 bytes. Then await file.seek(0) resets the file pointer to the start. The second await file.read() reads the full 50 bytes again. So both reads return 50 bytes.
Choose the correct statement about UploadFile in FastAPI.
Consider how UploadFile manages file data and async support.
UploadFile provides an async file-like interface. It stores the uploaded file in a temporary location on disk or in memory depending on size. It does not load the entire file into memory immediately (B is false). It supports async methods like read() (C is false). It does not save files permanently automatically (D is false).