Skip to content
tezvyn:
Menu

Implement a FastAPI file upload endpoint with form data

Source: fastapi.tiangolo.comMediumHow cards are made

Tests FastAPI multipart literacy. A strong answer names python-multipart, uses Annotated[UploadFile, File()] for the image, and Annotated[str, Form()] for user_id.

What's really being asked

The interviewer wants to see that you know FastAPI routes incoming request data based on type metadata, not just raw Python types. Specifically they care whether you understand that a file upload plus form fields requires multipart form-data parsing, which FastAPI delegates to python-multipart. They also want to see that you distinguish between in-memory bytes and UploadFile for larger or spooled files, and that you know the exact import pattern using Annotated.

The full answer

First, state that you must install python-multipart because FastAPI relies on it for multipart parsing. Second, import File and Form from fastapi alongside UploadFile. Third, show the function signature using Annotated: Annotated[UploadFile, File()] for the profile picture and Annotated[str, Form()] for the user_id. Fourth, explain the difference between bytes and UploadFile: bytes loads the entire file into memory, while UploadFile provides a SpooledTemporaryFile, a file name, and a content type, making it safer for images above a few megabytes. Fifth, mention that the endpoint should be async so you can await file read operations without blocking the event loop.

The mistakes people make

A major red flag is writing the parameter as user_id: str or file: UploadFile without the File() or Form() metadata. Without those wrappers FastAPI assumes the data arrives as JSON in the request body, which will break a multipart upload. Another red flag is forgetting python-multipart entirely and treating the file as a base64 string inside a JSON payload, which is inefficient and non-idiomatic. Candidates also stumble by using standard Pydantic models for the file field, which does not work for raw binary uploads in FastAPI.

What usually comes next

The interviewer may ask how you would validate file size or content type before processing. They might ask how to handle multiple file uploads at once, which is done with a list of Annotated[UploadFile, File()]. They could also ask about streaming large files to external storage like S3 rather than reading everything into memory, or how you would write tests for this endpoint using TestClient with files and data dictionaries.

A concrete example

You would write: from typing import Annotated; from fastapi import FastAPI, File, Form, UploadFile; app = FastAPI(); @app.post("/profile") async def update_profile(file: Annotated[UploadFile, File()], user_id: Annotated[str, Form()]): return {"filename": file.filename, "user_id": user_id}. Before running this, you install python-multipart. The File() metadata tells FastAPI to pull the binary from the multipart stream, while Form() pulls the text field, keeping both out of the JSON body parser.

Interview question

A FastAPI endpoint with parameters file: UploadFile and user_id: str fails when receiving multipart form-data. What is the most likely reason?

  • a.FastAPI assumes both parameters come from the JSON body because they lack File() and Form() metadata.Correct
  • b.The endpoint must use bytes instead of UploadFile to enable spooled temporary file handling.
  • c.String fields in multipart requests must always be defined as Pydantic models rather than plain types.
  • d.python-multipart is not installed, so FastAPI silently falls back to JSON parsing for binary data.
Why?

Without File() or Form() metadata, FastAPI assumes parameters arrive in the JSON body, which breaks multipart form-data parsing. Distractor A reverses the memory behavior: UploadFile spools large files, while bytes loads everything into memory.

Just read this? Test yourself on what you have been reading.

Read the original → fastapi.tiangolo.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on fastapi — each one lists the topics its interview covers.

See open roles