Bird
0
0

Given this middleware code snippet, what will be printed when a request is processed?

medium📝 component behavior Q13 of 15
FastAPI - Middleware and Hooks
Given this middleware code snippet, what will be printed when a request is processed?
class LogMiddleware:
    async def dispatch(self, request, call_next):
        print('Before request')
        response = await call_next(request)
        print('After request')
        return response
AOnly Before request printed
BBefore request printed, then After request printed after response
COnly After request printed
DNo output printed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze dispatch method flow

    Print 'Before request' runs before calling next handler, 'After request' runs after awaiting response.
  2. Step 2: Understand async call_next behavior

    call_next processes the request and returns response; code after await runs after response is ready.
  3. Final Answer:

    Before request printed, then After request printed after response -> Option B
  4. Quick Check:

    Print before and after call_next = B [OK]
Quick Trick: Code before await runs first, after await runs last [OK]
Common Mistakes:
MISTAKES
  • Thinking 'After request' prints before response
  • Ignoring async await order
  • Assuming only one print runs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes