Bird
0
0

You want to add middleware that logs request paths and modifies the response to add a custom header. Which approach correctly extends functionality using middleware?

hard📝 Application Q15 of 15
Flask - Middleware and Extensions
You want to add middleware that logs request paths and modifies the response to add a custom header. Which approach correctly extends functionality using middleware?
AModify the Flask app routes to log and add headers directly inside each route function
BUse middleware that only logs requests but does not call the app
CCreate middleware that logs <code>environ['PATH_INFO']</code>, calls the app, then adds header to response before returning
DAdd a middleware that replaces the response with a fixed string ignoring the app
Step-by-Step Solution
Solution:
  1. Step 1: Understand middleware responsibilities

    Middleware should handle logging before calling the app and modify the response after.
  2. Step 2: Evaluate options

    Create middleware that logs environ['PATH_INFO'], calls the app, then adds header to response before returning correctly logs the request path, calls the app, then modifies the response headers. Others either bypass app or do tasks incorrectly.
  3. Final Answer:

    Create middleware that logs environ['PATH_INFO'], calls the app, then adds header to response before returning -> Option C
  4. Quick Check:

    Middleware logs and modifies response = B [OK]
Quick Trick: Middleware wraps app: log before, modify response after [OK]
Common Mistakes:
MISTAKES
  • Modifying routes instead of middleware
  • Not calling the wrapped app
  • Replacing response ignoring app output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes