FastAPI exercises#

← Back to FastAPI

Three isolated reps, one per idea from the notes: a request body model, a path parameter, and a response model. Each stands on its own; use one as a smaller rep of a single idea before or after ../project/, the actual target of this sub-step.

  1. 01_request_body_model.py, matching ../notes/02-request-bodies-and-validation.md: define a pydantic model and use it as a request body.
  2. 02_path_parameter.py, matching ../notes/03-path-and-query-parameters.md: add a type-hinted path parameter and watch FastAPI convert and validate it for you.
  3. 03_response_model.py, matching ../notes/04-response-models-and-docs.md: add a second model describing the response, separate from the request body model.

Each file has its task in a docstring at the top and a # your code here marker. These run as live servers, not plain scripts: from this exercises/ folder, fastapi dev 01_request_body_model.py (adjust the filename), then use the interactive page at http://127.0.0.1:8000/docs to test it, the same way ../project/ gets tested. Install FastAPI first if you haven't: pip install "fastapi[standard]".

There's no solutions/ folder here, same as the rest of this module: each task is small enough, and each note it matches is short enough, that writing your own and rereading the note if something doesn't click is the actual exercise.