Bird
0
0

Consider this middleware snippet in Flask:

medium📝 component behavior Q4 of 15
Flask - Middleware and Extensions
Consider this middleware snippet in Flask:
class ResponseModifierMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        def custom_start(status, headers, exc_info=None):
            headers.append(('X-Processed', 'True'))
            return start_response(status, headers, exc_info)
        return self.app(environ, custom_start)

What effect does this middleware have when a request is processed?
AIt modifies the request path before Flask handles it
BIt blocks requests that do not have the 'X-Processed' header
CIt adds a custom header 'X-Processed: True' to every HTTP response
DIt logs the request body to the console
Step-by-Step Solution
Solution:
  1. Step 1: Analyze custom_start function

    The function appends a header ('X-Processed', 'True') to the response headers.
  2. Step 2: Understand middleware call

    The middleware calls the wrapped app with the modified start_response, ensuring the header is added to all responses.
  3. Final Answer:

    It adds a custom header 'X-Processed: True' to every HTTP response -> Option C
  4. Quick Check:

    Middleware modifies response headers [OK]
Quick Trick: Middleware can add headers by wrapping start_response [OK]
Common Mistakes:
MISTAKES
  • Confusing request modification with response modification
  • Assuming middleware blocks requests without that header
  • Thinking it logs request data

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes