Bird
0
0

Identify the error in this async middleware code:

medium📝 Debug Q14 of 15
Django - Async Django
Identify the error in this async middleware code:
class HeaderMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    async def __call__(self, request):
        response = self.get_response(request)
        response['X-Custom'] = 'Value'
        return response
AAsync __call__ cannot return response
BCannot modify response headers in middleware
CMissing await before self.get_response(request)
DMissing async keyword in __init__
Step-by-Step Solution
Solution:
  1. Step 1: Check async call to get_response

    Since __call__ is async, get_response must be awaited if it returns a coroutine.
  2. Step 2: Identify missing await

    The code calls self.get_response(request) without await, causing a coroutine object instead of response.
  3. Final Answer:

    Missing await before self.get_response(request) -> Option C
  4. Quick Check:

    Async call needs await before get_response [OK]
Quick Trick: Await async calls inside async methods [OK]
Common Mistakes:
MISTAKES
  • Forgetting await on async get_response
  • Thinking response headers can't be changed
  • Adding async to __init__ method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes