verify_json module¶
Functions in this module are checked to verify that either the request or the response of a Flask route Function is a JSON.
-
flask_verify.verify_json.verify_json_request(must_contain: Optional[Iterable] = None) → Callable¶ Verify that the Request’s type is JSON and contains necessary keys.
Verify that a request sent to a Flask route has a request of MIME type
application/jsonand if defined check if it contains the necessary keys.- Parameters
must_contain – If provided, JSON collection is checked if it contains items provided, if not, it will return 400.
- Returns
A view route that checks JSON requirements before executing requests.
-
flask_verify.verify_json.verify_json_response(route: Union[Callable[[…], tuple], Callable[[…], flask.wrappers.Response]]) → Callable[[…], flask.wrappers.Response]¶ Verify that a Flask route returns a JSON Response.
Effectively confirm the type of the Response by converting the return value of the route function to a JSON Response if it is not.
- Parameters
route – A route view function that either returns a Response itself or a tuple[JSONSerializable, int] where its first value is the response body and second value is the status code. The response body may also be dataclass object.
- Returns
A route function that either returns the same Response as the original route function if it returned a Response or a Response whose status code and body is taken from the tuple that is the return type of the original view function.
Warning
There is an old security vulnerability that affects JSON responses with top-level arrays, my understanding is that this mostly patched as functions such as
jsonifythat once blocked the user from converting top-level arrays no longer do so. Despite this, to err in the side of caution, this decorator will issue a waring if the user attempts to return a top-level array.
-
flask_verify.verify_json.verify_json_route(must_contain: Optional[Iterable] = None) → Callable¶ Wrapper around
verify_json_request()andverify_json_response().Wrapper around JSON response and request verification decorators, a decorated that is decorated with this decorator will be verified if it has a JSON request and its response will be converted into a JSON response.
- Parameters
must_contain – If specified, JSON request will be checked to contain this keys. If it does not, route will return a 400 error.
See also