From a8f764c1612ab17eb99924ac2363c05110a1911d Mon Sep 17 00:00:00 2001 From: Hugh McNamara Date: Tue, 19 Sep 2017 18:12:53 +0100 Subject: [PATCH] make method instead of property for alternative json decoding of request --- sanic/request.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/sanic/request.py b/sanic/request.py index aa778a8d..74200fe0 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -69,10 +69,10 @@ class Request(dict): self.stream = None @property - def json(self, loads=json_loads): + def json(self): if self.parsed_json is None: try: - self.parsed_json = loads(self.body) + self.parsed_json = json_loads(self.body) except Exception: if not self.body: return None @@ -80,6 +80,16 @@ class Request(dict): return self.parsed_json + def load_json(self, loads=json_loads): + try: + self.parsed_json = loads(self.body) + except Exception: + if not self.body: + return None + raise InvalidUsage("Failed when parsing body as json") + + return self.parsed_json + @property def token(self): """Attempt to return the auth header token.