StaffCoder
FastAPI
FastAPIFastAPI 0.139Python 3.13
7 modules · 28 challengesSign up
Module 1 · Path Operations & Routing

Route Order And Fixed Paths

easy100%1 of 1 solved

Problem statement

The /users/me page broke the day someone added /users/{user_id}: now me is read as a user id. Declare the routes so the fixed path and the parameterized path both work, and add a file route that keeps nested slashes.

Requirements

The app, the GET / home route and the three handlers, read_user, read_current_user and read_file_path, are already provided, together with what they return. Your task has one part, in app/main.py: make each of the three handlers a path operation.

FastAPI checks routes in the order they appear in the file and uses the first one that matches, so a parameterized path that comes first also catches a fixed path like /users/me. The path converter lets one path parameter take a value that contains slashes.

  • read_current_user handles GET /users/me and returns {"username": "current_user", "is_current": true}. It comes before /users/{user_id} in the file, so me is never read as a user id
  • read_user handles GET /users/{user_id}. /users/42 returns {"user_id": 42, "username": "user_42", "is_current": false}
  • read_file_path handles GET /files/{file_path} with the path converter, so it captures a nested path with all its slashes: /files/a/b/c/d.txt returns {"file_path": "a/b/c/d.txt"}
Sample Requests & Responses

GET /users/me

Response (200 OK):

{"username": "current_user", "is_current": true}

GET /users/42

Response (200 OK):

{"user_id": 42, "username": "user_42", "is_current": false}

GET /files/a/b/c/d.txt

Response (200 OK):

{"file_path": "a/b/c/d.txt"}

More in Path Operations & Routing

See the module
Typed Path ParametersThis problem covers reading the values that sit inside a web address, so a caller who puts something unexpected there is turned away before any of your code runs.89%8 of 9 solvedeasyEnum Constrained CategoriesThis problem covers limiting part of an address to a known list of choices, so a typo is refused and told what was allowed rather than quietly answering with nothing.100%1 of 1 solvedeasyStorage Browser RoutesThis problem covers laying out the whole set of addresses a file browser needs, so each one reaches the handler it was meant for and none of them quietly swallows another.67%2 of 3 solvedeasy

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.