def expect_json(view_function):
 @wraps(view_function)
 def parse_json_into_request(request, *args, **kwargs):
  if (('application/json' in request.META.get('CONTENT_TYPE', '')) and request.body):
   try:
    request.json = json.loads(request.body)
   except ValueError:
    return JsonResponseBadRequest({'error': 'Invalid JSON'})
  else:
   request.json = {}
  return view_function(request, *args, **kwargs)
 return parse_json_into_request

def expect_json ( view_function ) : @ wraps ( view_function ) def parse_json_into_request ( request , * args , ** kwargs ) : if ( ( 'application/json' in request . META . get ( 'CONTENT_TYPE' , '' ) ) and request . body ) : try : request . json = json . loads ( request . body ) except ValueError : return JsonResponseBadRequest ( { 'error' : 'Invalid JSON' } ) else : request . json = { } return view_function ( request , * args , ** kwargs ) return parse_json_into_request
view decorator for simplifying handing of requests that expect json .