handle ill-formed JSON
This commit is contained in:
parent
9883782f3a
commit
887dc95e3d
1 changed files with 6 additions and 2 deletions
|
@ -32,8 +32,12 @@ async def handler(request):
|
||||||
This one handles a POST, checks its content, and forwards it to the matrix room.
|
This one handles a POST, checks its content, and forwards it to the matrix room.
|
||||||
"""
|
"""
|
||||||
data = await request.read()
|
data = await request.read()
|
||||||
|
try:
|
||||||
data = json.loads(data.decode())
|
data = json.loads(data.decode())
|
||||||
status, ret = HTTPStatus.BAD_REQUEST, 'I need a json dict with text & key'
|
status, ret = HTTPStatus.BAD_REQUEST, 'I need a json dict with text & key'
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
data = {}
|
||||||
|
status, ret = HTTPStatus.BAD_REQUEST, 'This was not valid JSON'
|
||||||
if all(key in data for key in ['text', 'key']):
|
if all(key in data for key in ['text', 'key']):
|
||||||
status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"'
|
status, ret = HTTPStatus.UNAUTHORIZED, 'I need the good "key"'
|
||||||
if data['key'] == API_KEY:
|
if data['key'] == API_KEY:
|
||||||
|
|
Loading…
Reference in a new issue