Bird
0
0

Identify the error in this middleware code snippet:

medium📝 Debug Q14 of 15
Flask - Middleware and Extensions
Identify the error in this middleware code snippet:
class BadMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, request):
        print('Middleware active')
        return self.app(request)
AMissing __init__ method to accept app
B__call__ method has wrong parameters; should be (environ, start_response)
CMiddleware class should not print anything
DThe return statement should be outside __call__
Step-by-Step Solution
Solution:
  1. Step 1: Check __call__ method signature

    WSGI middleware __call__ must accept (environ, start_response), not (request).
  2. Step 2: Validate other parts

    __init__ is correct, printing is allowed, return inside __call__ is correct.
  3. Final Answer:

    __call__ method has wrong parameters; should be (environ, start_response) -> Option B
  4. Quick Check:

    Middleware __call__ signature = (environ, start_response) [OK]
Quick Trick: Middleware __call__ needs environ and start_response params [OK]
Common Mistakes:
MISTAKES
  • Using Flask request object instead of WSGI params
  • Omitting start_response parameter
  • Misplacing return statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes