Last active
April 20, 2016 06:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting