1

I am looking for the right directions to add a custom field in the HTTP response using middleware and access the custom field in the JavaScript front-end.

I am trying to implement this, but on receiving the response on the JavaScript side there is no field like "is_logged" in the body.

class SimpleMiddleware:
def __init__(self, get_response):
    self.get_response = get_response

def __call__(self, request):

    if request.user.is_authenticated:
        response = self.get_response(request)
        response.body['is_logged'] = True
    else:
        response = self.get_response(request)
        response.body['is_logged'] = False

    return response
vardos
  • 334
  • 3
  • 13
  • How do you want to "pass" this item here? As a cookie? Another header? – Willem Van Onsem Jan 27 '19 at 01:46
  • @WillemVanOnsem either is fine as long as I am able to access it at the front-end – vardos Jan 27 '19 at 01:47
  • Well the body is, iirc, the stream of body of the HTTP response. You can set a head, just like `response['is_logged'] = '1'` for example. You can verify that this works by making a request in the Network Console of FF/GC. – Willem Van Onsem Jan 27 '19 at 01:49
  • @WillemVanOnsem it works in the Network Console. Is there any way to access at the JavaScript end? – vardos Jan 27 '19 at 02:11
  • You read the corresponding *response header*: https://stackoverflow.com/questions/1557602/jquery-and-ajax-response-header – Willem Van Onsem Jan 27 '19 at 02:12

0 Answers0