StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 5 · Form Data, Files & Cookies/Headers

Validated File Upload

easy—No data yet

Problem statement

Build an import endpoint that takes an uploaded file alongside a description, turns away a kind of file it does not accept and one that is too big, and a batch endpoint that reports every file it is handed.

API Endpoints to Implement

app/services/imports.py holds the two limits: ALLOWED_CONTENT_TYPES, which is text/csv and application/json, and MAX_IMPORT_BYTES, which is 1024.

POST /imports

  • file arrives as an uploaded file and description as a form field, text. Both are required, and both are parts of one multipart submission.
  • A file whose declared content type is not one of the allowed two is answered with 415 and the body {"detail": "Unsupported content type application/pdf; expected text/csv or application/json"}, naming the type that was sent. The declared type decides this, not the file name, so a .bin sent as text/csv is accepted and a .csv sent as image/png is not.
  • A file longer than MAX_IMPORT_BYTES is answered with 413 and the body {"detail": "File bulk.csv exceeds the 1024 byte limit"}, naming the file. A file of exactly 1024 bytes is accepted; one byte more is not.
  • Response code: 200. The body holds filename, content_type, size, the number of bytes received, and description as it was sent.

POST /imports/batch

  • files arrives as a list of uploaded files, required, and carries no type or size limit.
  • Response code: 200. The body holds count, how many files arrived, and files, one entry per file in the order they were sent, each holding filename, content_type and size.

Rejected requests

A missing part is answered with 422 and one detail entry per absent part, carrying loc of ["body", "file"], ["body", "description"] or ["body", "files"], an input of null, and the type and msg reported for the rule.

The uploads must be declared on the handlers rather than read out of the request, since the service publishes the contract at GET /openapi.json, where each request body is required, is offered only as multipart, and points at a named schema of its own: the import naming file and description as required with file carried as binary, and the batch naming files as a required list of binary.

Sample Requests & Responses

POST /imports, sending readings.csv of 15 bytes declared as text/csv with a description

Response (200 OK):

{
  "filename": "readings.csv",
  "content_type": "text/csv",
  "size": 15,
  "description": "Daily readings"
}

POST /imports, sending report.pdf declared as application/pdf

Response (415 Unsupported Media Type):

{
  "detail": "Unsupported content type application/pdf; expected text/csv or application/json"
}

POST /imports, sending bulk.csv of 4096 bytes declared as text/csv

Response (413 Content Too Large):

{
  "detail": "File bulk.csv exceeds the 1024 byte limit"
}

POST /imports/batch, sending north.csv of 10 bytes and totals.json of 37 bytes

Response (200 OK):

{
  "count": 2,
  "files": [
    {"filename": "north.csv", "content_type": "text/csv", "size": 10},
    {"filename": "totals.json", "content_type": "application/json", "size": 37}
  ]
}

More in Form Data, Files & Cookies/Headers

See the module
Login Form FieldsThis problem covers signing a member in from the values a browser form submits, so a matching pair is greeted by name and a wrong one is turned away without saying which half was wrong.—No data yeteasyHeader And Cookie InputsThis problem covers reading the small pieces of context a client sends alongside a request, its version and its chosen release channel, and answering with both a body and headers of its own.—No data yeteasyDocument Portal IntakeThis problem covers taking a submission that arrives in several pieces at once, a form, files, a header and a cookie, routing it to the right desk, and refusing the pieces that break a rule.—No data yeteasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.