Browse Source

Added a property is_body_json

The above property returns True if the request body contains valid json, if the body doesn't contains valid json then it returns false. This can be used in the situations when we want to validate the json of body.
pull/1940/head
smit thakkar 8 years ago committed by GitHub
parent
commit
a3a2de1fc4
  1. 13
      flask/wrappers.py

13
flask/wrappers.py

@ -121,7 +121,20 @@ class Request(RequestBase):
if mt.startswith('application/') and mt.endswith('+json'):
return True
return False
@property
def is_body_json(self):
"""
Validates wether the body consists valid json or not.
"""
request_charset = self.mimetype_params.get('charset')
try:
json.loads(request_charset)
except ValueError, e:
return False
return True
def get_json(self, force=False, silent=False, cache=True):
"""Parses the incoming JSON request data and returns it. By default
this function will return ``None`` if the mimetype is not

Loading…
Cancel
Save