Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSONRequestMiddleware
import json
from .utils import copy_body
class JSONRequest(dict):
def __init__(self, request):
try:
data = json.loads(copy_body(request))
self.is_valid = True
except ValueError as e:
data = {}
self.is_valid = False
self.exception = e
super(JSONRequest, self).__init__(data)
def __nonzero__(self):
return self.is_valid
class JSONRequestMiddleware(object):
def process_request(self, request):
request.JSON = JSONRequest(request)
import io
def copy_body(request):
data = getattr(request, '_body', request.body)
request._body = data
request._stream = io.BytesIO(data)
request._files = None
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.