-
-
Save DarkSector/11426cb36d8efa6d90d9d264225d7705 to your computer and use it in GitHub Desktop.
JSONRequestMiddleware
This file contains hidden or 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
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) |
This file contains hidden or 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
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