Skip to content

Instantly share code, notes, and snippets.

@abhillman
Last active April 20, 2016 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhillman/6910689 to your computer and use it in GitHub Desktop.
Save abhillman/6910689 to your computer and use it in GitHub Desktop.
Django Middleware to add request.JSON to any HTTP requests that (1) are ajax and (2) are application/json content type.
from django.utils import simplejson
class JSONMiddleware(object):
def process_request(self, request):
request.JSON = None
if request.is_ajax():
if request.META.get('CONTENT_TYPE').count('application/json'):
try:
request.JSON = simplejson.loads(request.body)
except simplejson.JSONDecodeError:
pass
@Aburoko
Copy link

Aburoko commented Apr 20, 2016

Interesting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment