Add remember headers to json response using json renderer

authentication, json, pyramid, python

Solution

You can set that information on the response directly, as documented in the Vary Attributes of Rendered Responses section of the Pyramid manual:

@view_config(route_name='login', renderer='json', request_method='POST')
def post_login(request):
   ...
   ... authentication logic
   ...
   headers = remeber(request, login)
   request.response.headerlist.extend(headers)
   return { 'successful': True, 'message': 'auth OK'}

Problem

this is the current way I use to add remeber headers to response: ``` @view_config(route_name='login', renderer='json', request_method='POST') def post_login(request): ... ... authentication logic ... headers = remeber(request, login) return HTTPFound(location=came_from, headers=headers) ``` but my js is waiting for the response {successful: True, message: 'auth OK'}. HTTPFound will redirect to came_from. I want js redirect so I tried this ``` @view_config(route_name='login', renderer='json', request_method='POST') def post_login(request): ... ... authentication logic ... return { 'successful': True, 'message': 'auth OK'} ``` but since the remeber headers are never added to the response it will be never authenticated on the other side of the moooon. so my question is how to add those remeber headers using json renderer?

Original source