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

Typed Path Parameters

easy89%8 of 9 solved

Problem statement

Without types, item URLs accept anything: /items/abc reaches the handler as the text "abc". Declare the routes with typed path parameters, so FastAPI converts each value and rejects a bad one before the handler runs.

Requirements

The app, the GET / home route and the three handlers, read_item, delete_item and read_user, 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.

A path operation connects an HTTP method and a path to a handler, so one path can lead to different handlers for different methods. When a path parameter has a type, FastAPI converts the text from the URL to that type, and rejects a value that does not fit before the handler runs.

  • read_item handles GET /items/{item_id}, with item_id typed as int. /items/123 returns {"item_id": 123}, with the id as a number
  • A non-numeric id, such as /items/abc, is rejected by FastAPI with a 422 validation error, and read_item does not run
  • delete_item handles DELETE on the same path, /items/{item_id}, with item_id typed as int. DELETE /items/5 returns {"deleted": 5}
  • read_user handles GET /users/{username}, with username typed as str. It accepts any text, even text that looks like a number: /users/123 returns {"username": "123"}
  • Any other method on these paths answers 405, for example POST /items/5 or DELETE /users/bob
Sample Requests & Responses

GET /items/123

Response (200 OK):

{"item_id": 123}

GET /items/abc

Response (422 Unprocessable Entity):

{"detail": [{"type": "int_parsing", "loc": ["path", "item_id"], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "abc"}]}

DELETE /items/5

Response (200 OK):

{"deleted": 5}

GET /users/john.doe

Response (200 OK):

{"username": "john.doe"}

GET /users/123

Response (200 OK):

{"username": "123"}

POST /items/5

Response (405 Method Not Allowed):

{"detail": "Method Not Allowed"}

More in Path Operations & Routing

See the module
Route Order And Fixed PathsThis problem covers what happens when two addresses could both match the same request, so a page with a fixed name keeps working after a more general pattern is added beside it.100%1 of 1 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.