Bird
0
0

How can you modify the middleware to add timing info only for GET requests?

hard🚀 Application Q9 of 15
FastAPI - Middleware and Hooks
How can you modify the middleware to add timing info only for GET requests?
ACheck if request.method == 'GET' before timing and header addition
BAdd timing header for all methods unconditionally
CUse a dependency to filter GET requests
DModify the route handlers instead of middleware
Step-by-Step Solution
Solution:
  1. Step 1: Access request method in middleware

    Use request.method to check HTTP method inside dispatch.
  2. Step 2: Conditionally add timing

    Only calculate and add timing header if method is 'GET'.
  3. Final Answer:

    Check if request.method == 'GET' before timing and header addition -> Option A
  4. Quick Check:

    Filter by method inside middleware [OK]
Quick Trick: Use request.method to conditionally add headers [OK]
Common Mistakes:
MISTAKES
  • Adding timing for all methods
  • Using dependencies instead of middleware check
  • Changing route handlers unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes