Consider this FastAPI middleware that logs request method and path. What will it print when a GET request is made to /items/5?
from fastapi import FastAPI, Request from starlette.middleware.base import BaseHTTPMiddleware app = FastAPI() class LogMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): print(f"Request: {request.method} {request.url.path}") response = await call_next(request) return response app.add_middleware(LogMiddleware) @app.get('/items/{item_id}') async def read_item(item_id: int): return {"item_id": item_id}
Look at the request.method and request.url.path values used in the print statement.
The middleware prints the HTTP method and the exact path of the request. For a GET request to /items/5, it prints Request: GET /items/5.
Which option correctly identifies the syntax error in this middleware code?
from fastapi import FastAPI, Request from starlette.middleware.base import BaseHTTPMiddleware app = FastAPI() class ErrorMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): response = await call_next(request) return response app.add_middleware(ErrorMiddleware) @app.get('/') async def root(): return {"message": "Hello"}
Check the parentheses in the app.add_middleware line.
The line app.add_middleware(ErrorMiddleware is missing a closing parenthesis, causing a syntax error.
This middleware adds a custom header X-Custom with value 42. What will the response headers contain after a request?
from fastapi import FastAPI, Request from starlette.middleware.base import BaseHTTPMiddleware app = FastAPI() class HeaderMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): response = await call_next(request) response.headers["X-Custom"] = "42" return response app.add_middleware(HeaderMiddleware) @app.get('/') async def root(): return {"hello": "world"}
Look at how the header is added to the response object.
The middleware sets the header 'X-Custom' to '42' exactly as shown, so the response headers include that key and value.
Examine this middleware code. Why does it cause a runtime error when a request is made?
from fastapi import FastAPI, Request from starlette.middleware.base import BaseHTTPMiddleware app = FastAPI() class FaultyMiddleware(BaseHTTPMiddleware): async def dispatch(self, request: Request, call_next): response = call_next(request) # Missing await return response app.add_middleware(FaultyMiddleware) @app.get('/') async def root(): return {"msg": "ok"}
Check how call_next is called inside dispatch.
call_next is an async function and must be awaited. Missing await causes a runtime error because the coroutine is not awaited.
Given multiple middlewares added in this order: MiddlewareA, then MiddlewareB, which statement best describes the order of dispatch calls and response returns?
Think of middleware as layers wrapping the app, like nested boxes.
FastAPI calls middleware dispatch methods in the order they are added for the request. The response flows back in reverse order, so MiddlewareA dispatch runs first on request and last on response.