In FastAPI, what does @app.get("/items/{item_id}") above a function do?
Options
- It downloads an item from the server to the client
- It registers a GET route;
item_idis taken from the URL (Correct option) - It creates a database table named items for the function
- It defines a test case for the function underneath it
Answer
Registers a route: GET requests to /items/<id> are handled by that function, with item_id taken from the URL
Why
Routing connects a URL + HTTP method to a Python function (an endpoint).
When a request GET /items/7 arrives, FastAPI calls your function with item_id=7, converting it to the type you declared (e.g. int).
Whatever the function returns (dict, Pydantic model) is turned into the JSON response.
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"id": item_id}Remember
Route = method + path → function.