Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
File upload (single file)
📖 Scenario: You are building a simple web API that allows users to upload a single file. This is common in many web applications where users need to send images, documents, or other files to the server.
🎯 Goal: Create a FastAPI app that accepts a single file upload through a POST request and returns the filename and content type.
📋 What You'll Learn
Use FastAPI to create the web app
Create an endpoint to accept a single file upload
Use the correct type hint for the uploaded file
Return a JSON response with the filename and content type
💡 Why This Matters
🌍 Real World
Uploading files is common in web apps for user profile pictures, documents, or media content.
💼 Career
Backend developers often implement file upload endpoints using FastAPI or similar frameworks.
Progress0 / 4 steps
1
Create FastAPI app instance
Import FastAPI from fastapi and create an app instance called app.
FastAPI
Hint
Use app = FastAPI() to create the app instance.
2
Import UploadFile and File
Import UploadFile and File from fastapi to handle file uploads.
FastAPI
Hint
Import both UploadFile and File from fastapi in the same line.
3
Create POST endpoint for file upload
Create a POST endpoint at /uploadfile/ using @app.post. Define an async function called upload_file that accepts a parameter file of type UploadFile using File(...).
FastAPI
Hint
Use @app.post('/uploadfile/') decorator and define an async function with the correct parameter type.
4
Return filename and content type
Inside the upload_file function, return a dictionary with keys filename and content_type using file.filename and file.content_type.
FastAPI
Hint
Return a dictionary with the file's filename and content_type.
Practice
(1/5)
1. In FastAPI, which parameter type is used to accept a single uploaded file in an endpoint?
easy
A. List[UploadFile]
B. str
C. UploadFile
D. int
Solution
Step 1: Understand FastAPI file upload types
FastAPI uses UploadFile to handle file uploads efficiently.
Step 2: Identify single file upload parameter
For a single file, the parameter type is UploadFile, not a list or primitive type.
Final Answer:
<code>UploadFile</code> -> Option C
Quick Check:
Single file upload uses UploadFile [OK]
Hint: Use UploadFile for single file upload in FastAPI [OK]
Common Mistakes:
Using str or int instead of UploadFile
Using List[UploadFile] for single file
Not importing UploadFile from fastapi
2. Which of the following is the correct way to declare a FastAPI endpoint parameter to accept a single file upload?
easy
A. file: str = File(...)
B. file: bytes = File(...)
C. file: UploadFile
D. file: UploadFile = File(...)
Solution
Step 1: Check parameter declaration for file upload
FastAPI requires File(...) to mark the parameter as a file upload.
Step 2: Match type with File marker
The type must be UploadFile combined with File(...) for single file upload.
Final Answer:
file: UploadFile = File(...) -> Option D
Quick Check:
UploadFile with File(...) is correct syntax [OK]
Hint: Use UploadFile = File(...) to accept single file [OK]
Common Mistakes:
Omitting File(...) marker
Using str or bytes instead of UploadFile
Not assigning default File(...)
3. What will be the output of this FastAPI endpoint when a file named 'test.txt' with content 'hello' is uploaded?
C. file.read() should be awaited in async function
D. Endpoint function must be async to await file.read()
Solution
Step 1: Check imports and function signature
The code misses importing File and the function is not async but calls file.read() which is async.
Step 2: Identify async usage and await
To read file content, function must be async and use await file.read().
Final Answer:
All of the above -> Option B
Quick Check:
Missing import, async, and await cause errors [OK]
Hint: Import File, make function async, await file.read() [OK]
Common Mistakes:
Forgetting to import File
Using sync function with async file.read()
Not awaiting file.read()
5. You want to create a FastAPI endpoint that accepts a single file upload and returns the file's name, content type, and first 10 bytes as a UTF-8 string. Which code snippet correctly implements this?