diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-08-10 19:16:02 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-08-10 19:16:02 +0300 |
commit | bf8cec8ec4adb39b716e8d93b2afb8f7f0e43042 (patch) | |
tree | 41e1dee1c073c3f195e87a6353e0b2d279c2deb0 | |
parent | e2c5595f35c398f216c2e0ee6a805722cc2f9582 (diff) | |
download | apifw-bf8cec8ec4adb39b716e8d93b2afb8f7f0e43042.tar.gz |
Add: parse a JSON body ourselves, for better errors
-rw-r--r-- | apifw/bottleapp.py | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/apifw/bottleapp.py b/apifw/bottleapp.py index 9b45073..40be756 100644 --- a/apifw/bottleapp.py +++ b/apifw/bottleapp.py @@ -14,6 +14,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. +import json import logging import re import time @@ -201,11 +202,14 @@ class BottleApplication: def _get_request_body(self): content_type = bottle.request.get_header('Content-Type') - if content_type == 'application/json': - body = bottle.request.json - else: - body = bottle.request.body.read() - return content_type, body + raw_body = bottle.request.body.read() + if raw_body != 'application/json': + return content_type, raw_body + + try: + return content_type, json.loads(raw_body) + except json.decoder.JSONDecodeError as e: + raise bottle.HTTPError(status=apifw.HTTP_BAD_REQUEST, body=str(e)) def create_bottle_application(api, counter, logger, config): |