Given the following FastAPI app with APIRouter, what will be the JSON response when a client requests /items/42?
from fastapi import FastAPI, APIRouter router = APIRouter() @router.get('/{item_id}') async def read_item(item_id: int): return {"item_id": item_id, "name": f"Item {item_id}"} app = FastAPI() app.include_router(router, prefix='/items')
Check how the item_id parameter is typed and how the router prefix affects the route.
The route is defined with @router.get('/{item_id}') and the router is included with prefix /items. This means the full path is /items/{item_id}. Since the client requests /items/42, it matches and returns the JSON with item_id as integer 42 and the name.
Choose the correct way to include an APIRouter named user_router into a FastAPI app with the prefix /users.
from fastapi import FastAPI, APIRouter user_router = APIRouter() app = FastAPI()
Check the method name and parameter names for including routers.
The correct method to add a router is include_router with the router object as first argument and prefix as keyword argument. Option B matches this exactly.
Consider this FastAPI app code:
from fastapi import FastAPI, APIRouter
router = APIRouter()
@router.get('/profile')
async def get_profile():
return {"profile": "user"}
app = FastAPI()
app.include_router(router, prefix='/user')
@app.get('/user/profile')
async def direct_profile():
return {"profile": "direct"}When a client requests /user/profile, which response is returned and why?
Consider the order of route registration and how FastAPI resolves routes: routes are matched in the order they are registered (first registered matches first).
FastAPI matches routes in the order they are added to the app's internal route list. The router route /user/profile is added first when include_router is called. The direct app route /user/profile is added afterward via the decorator. The first matching route in the list is used, so the router route returns {"profile": "user"}.
Given these two router modules and main app, what JSON is returned when requesting /api/v1/products/5?
# products_router.py
from fastapi import APIRouter
router = APIRouter()
@router.get('/products/{product_id}')
async def get_product(product_id: int):
return {"product_id": product_id, "status": "ok"}
# main.py
from fastapi import FastAPI
from products_router import router as products_router
app = FastAPI()
app.include_router(products_router, prefix='/api/v1')Check how the prefix affects the full route path and the parameter type.
The router defines /products/{product_id}. The app includes it with prefix /api/v1. So the full path is /api/v1/products/{product_id}. Requesting /api/v1/products/5 matches and returns the JSON with product_id as integer 5.
Choose the correct statement about using APIRouter in FastAPI for modular route design.
Think about modular design and route reuse with different prefixes.
APIRouter is designed to group routes and can be included multiple times with different prefixes or dependencies. This allows modular and reusable route design. Other options are false because routes can share path names if prefixes differ, parameters are scoped per route, and dependencies can be defined in routers.