[{"id":"fastapiintermediate-001","courseId":"fastapi-intermediate","domain":"routing","topic":"Path parameter types","prompt":"A route is declared as @app.get(\"/items/{item_id}\") and its function takes item_id: int. A client requests /items/3. What does the function receive for item_id?","hint":"The URL carries text. Look at what the type annotation asks FastAPI to do with it.","explain":"A value in the URL starts as plain text. A type annotation tells the framework what kind of value to build from that text before your code runs, and it rejects text it cannot build.","source":"https://fastapi.tiangolo.com/tutorial/path-params/","interaction":{"type":"single"},"options":[{"id":"a","text":"The Python int 3","why":"FastAPI converts the path segment to the annotated type, so the function gets the number 3 as an int, not as text."},{"id":"b","text":"The string \"3\"","whyNot":"The annotation changes that: the value is converted, so the function does not get the text \"3\"."},{"id":"c","text":"The float 3.0","whyNot":"The annotation asks for an int, so nothing turns the value into a float; a value like 4.2 is rejected with an error instead."},{"id":"d","text":"A dict such as {\"item_id\": \"3\"}","whyNot":"The function receives the value itself as its argument; a dict is only what the function may return."}],"answer":"a","explanation":"A path parameter is passed to the function as the argument of the same name, and FastAPI converts and validates it to the annotated type; an int annotation yields a Python int.","concept":"A path parameter is passed to the function as the argument of the same name, and FastAPI converts and validates it to the annotated type; an int annotation yields a Python int.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"@app.get(\"/items/{item_id}\")\nasync def read_item(item_id: int):\n    return {\"item_id\": item_id}","takeaways":["Path parameter annotations both convert and validate the URL value, and a value that cannot convert returns an error."]},{"id":"fastapiintermediate-002","courseId":"fastapi-intermediate","domain":"routing","topic":"Route order and path parameters","prompt":"A file declares @app.get(\"/users/{user_id}\") first, then @app.get(\"/users/me\") below it. A client sends GET /users/me. Which function runs?","hint":"Routes are checked in the order they are declared. Which pattern is reached first?","explain":"A framework tests each incoming request against its route patterns one by one. A pattern can hold a placeholder that accepts any text in its position, so declaration order can decide the winner.","source":"https://fastapi.tiangolo.com/tutorial/path-params/","interaction":{"type":"single"},"options":[{"id":"a","text":"read_user, with user_id set to \"me\"","why":"The first declared pattern matches /users/me, so it handles the request and receives \"me\" as user_id."},{"id":"b","text":"read_user_me, because a fixed path beats a placeholder","whyNot":"No such priority applies: paths are checked in order, so the first match wins and the later fixed path is never reached."},{"id":"c","text":"A 404 error, because the two routes conflict","whyNot":"Overlapping routes are not rejected; the first matching path operation handles the request."},{"id":"d","text":"Both functions run, one after the other","whyNot":"Only one path operation handles a request, and it is the first one that matches."}],"answer":"a","explanation":"Path operations are matched in declaration order and the first match is used, so a fixed path such as /users/me must be declared before a parameterised path such as /users/{user_id}.","concept":"Path operations are matched in declaration order and the first match is used, so a fixed path such as /users/me must be declared before a parameterised path such as /users/{user_id}.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"@app.get(\"/users/{user_id}\")\nasync def read_user(user_id: str):\n    ...\n\n@app.get(\"/users/me\")\nasync def read_user_me():\n    ...","takeaways":["Declare fixed paths such as /users/me before parameterised paths such as /users/{user_id}."]},{"id":"fastapiintermediate-003","courseId":"fastapi-intermediate","domain":"routing","topic":"Required and optional query parameters","prompt":"A teammate wants the client to always send the query parameter q, with no fallback value in the function. Which declaration does that?","hint":"Think about what the declaration tells FastAPI when the client leaves the value out.","explain":"A function argument that is not part of the URL template is read from the request. Its declaration may include a fallback value for when the request leaves it out, which changes what the client has to send.","source":"https://fastapi.tiangolo.com/tutorial/query-params/","interaction":{"type":"single"},"options":[{"id":"a","text":"q: str","why":"With no default value, the query parameter is required, so the client must send it."},{"id":"b","text":"q: str | None = None","whyNot":"A default of None makes the parameter optional, so the client can leave it out."},{"id":"c","text":"q: str = \"\"","whyNot":"Any default value, even an empty string, makes the parameter optional."},{"id":"d","text":"q: str = Query(default=None)","whyNot":"Setting Query's default to None still makes the parameter optional."}],"answer":"a","explanation":"A non-path function parameter with no default is a required query parameter; giving it any default, including None, makes it optional.","concept":"A non-path function parameter with no default is a required query parameter; giving it any default, including None, makes it optional.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Leave the default off a query parameter to make it required; a default such as None makes it optional."]},{"id":"fastapiintermediate-004","courseId":"fastapi-intermediate","domain":"routing","topic":"Where FastAPI reads each parameter","prompt":"A route is @app.put(\"/items/{item_id}\") with parameters item_id: int, item: Item (a Pydantic model) and q: str | None = None. Where does FastAPI read q from?","hint":"Check each parameter against the URL template, then against its type annotation.","explain":"FastAPI decides where each argument comes from by checking its name against the URL template, then its type annotation. A data-model class is handled differently from a plain type.","source":"https://fastapi.tiangolo.com/tutorial/query-params/","interaction":{"type":"single"},"options":[{"id":"a","text":"The URL query string, as an optional value","why":"A simple-typed parameter that is not in the path is a query parameter, and its default makes it optional."},{"id":"b","text":"The JSON request body, as a second field beside item","whyNot":"Only a parameter typed as a Pydantic model is read from the body, and q is a simple type."},{"id":"c","text":"The URL path, since it is declared in the same function as item_id","whyNot":"Only names that appear in the path template are taken from the path."},{"id":"d","text":"A request header named q","whyNot":"Headers need the Header() marker; without it, a simple-typed parameter is read as a query parameter."}],"answer":"a","explanation":"FastAPI matches each parameter in order: names in the path template come from the path, Pydantic model types come from the body, and other simple types come from the query string.","concept":"FastAPI matches each parameter in order: names in the path template come from the path, Pydantic model types come from the body, and other simple types come from the query string.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A simple-typed argument not in the path is a query parameter; a Pydantic model argument is read from the body."]},{"id":"fastapiintermediate-005","courseId":"fastapi-intermediate","domain":"routing","topic":"Mounting one router under several prefixes","prompt":"A team wants the same APIRouter, with its items endpoints, served under both /api/v1 and /api/latest. Which approach does FastAPI document for this?","hint":"Think about whether including a router is a one-time action, and where the prefix is supplied.","explain":"A group of endpoints can be served at several addresses without being copied or changed. Building the group and attaching it to the application are separate steps, and the address is chosen when attaching.","source":"https://fastapi.tiangolo.com/tutorial/bigger-applications/","interaction":{"type":"single"},"options":[{"id":"a","text":"Call app.include_router(router) once per prefix, with a different prefix each time","why":"The docs say include_router can be used several times with the same router and different prefixes, such as /api/v1 and /api/latest."},{"id":"b","text":"Copy the router's routes into a new APIRouter for each prefix","whyNot":"Copying is unnecessary, because one router can be included repeatedly under different prefixes."},{"id":"c","text":"Set one prefix on the router; each include_router call reuses it","whyNot":"Each include_router call can take its own prefix, so two prefixes need two calls with different values."},{"id":"d","text":"Only the first include_router call takes effect; later calls are ignored","whyNot":"The docs say include_router can be called multiple times with the same router, so later calls are not ignored."}],"answer":"a","explanation":"include_router adds a router's path operations to the app under a prefix you pass in; calling it again with the same router and another prefix exposes the same API under a second URL.","concept":"include_router adds a router's path operations to the app under a prefix you pass in; calling it again with the same router and another prefix exposes the same API under a second URL.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["One APIRouter can be included several times, each time with its own prefix."]}]