def returns_json(f):
"""Decorator to add the content type to responses."""
@wraps(f)
def decorated_function(*args, **kwargs):
try:
r = f(*args, **kwargs)
except HTTPException as e:
# monkey-patch the headers / body to be json
headers = e.get_headers()
for header in headers:
if 'Content-Type' in header:
headers.remove(header)
headers.append(('Content-Type', 'application/json'))
e.get_headers = lambda x: headers
e.get_body = lambda x: json.dumps({"message": e.description})
raise e
if isinstance(r, tuple):
return Response(r[0], status=r[1], content_type='application/json')
else:
return Response(r, content_type='application/json')
return decorated_function
评论列表
文章目录