diff options
author | Mark Haines <mark.haines@matrix.org> | 2016-03-11 16:41:03 +0000 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2016-03-11 16:41:03 +0000 |
commit | e9c1cabac26cf8e28152ebdb3caf29d4457eea0e (patch) | |
tree | 792ef6135a1a1a52f39a83a8e2ff6e2e28e949c7 /synapse/http | |
parent | Fix SQL statement (diff) | |
download | synapse-e9c1cabac26cf8e28152ebdb3caf29d4457eea0e.tar.xz |
Use parse_json_object_from_request to parse JSON out of request bodies
Diffstat (limited to 'synapse/http')
-rw-r--r-- | synapse/http/servlet.py | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/synapse/http/servlet.py b/synapse/http/servlet.py index 41c519c000..1996f8b136 100644 --- a/synapse/http/servlet.py +++ b/synapse/http/servlet.py @@ -128,14 +128,21 @@ def parse_json_object_from_request(request): if it wasn't a JSON object. """ try: - content = simplejson.loads(request.content.read()) - if type(content) != dict: - message = "Content must be a JSON object." - raise SynapseError(400, message, errcode=Codes.BAD_JSON) - return content + content_bytes = request.content.read() + except: + raise SynapseError(400, "Error reading JSON content.") + + try: + content = simplejson.loads(content_bytes) except simplejson.JSONDecodeError: raise SynapseError(400, "Content not JSON.", errcode=Codes.NOT_JSON) + if type(content) != dict: + message = "Content must be a JSON object." + raise SynapseError(400, message, errcode=Codes.BAD_JSON) + + return content + class RestServlet(object): |