def read_body_as_json(req):
"""Reads the request body and returns a dict with the content
If body is empty, returns a void python dictionary
:return: A dictionary or similar python object (list)
:rtype: Object
"""
try:
body_req = req.stream.read().decode('utf-8')
if body_req is None or body_req == "":
return {}
else:
return json.loads(body_req)
except (json.decoder.JSONDecodeError) as err:
msg = ("Please, read the documentation carefully and try again. "
"Couldn't decode the input stream (body).")
raise falcon.HTTPBadRequest(
title="Couldn't read body correctly from HTTP request",
description=str(msg))
评论列表
文章目录