From 2b4dbee33d35d546dd32f76315c0180bef9f62c7 Mon Sep 17 00:00:00 2001 From: pengshiyu <1940607002@qq.com> Date: Sat, 8 Jul 2023 17:54:52 +0800 Subject: [PATCH] force return json instead of raise error for request.json --- src/flask/wrappers.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/flask/wrappers.py b/src/flask/wrappers.py index ef7aa38c..fb2be389 100644 --- a/src/flask/wrappers.py +++ b/src/flask/wrappers.py @@ -51,6 +51,21 @@ class Request(RequestBase): #: something similar. routing_exception: Exception | None = None + @property + def json(self) -> t.Any | None: + """The parsed JSON data if :attr:`mimetype` indicates JSON + (:mimetype:`application/json`, see :attr:`is_json`). + + Calls :meth:`get_json` with default arguments. + + If the request content type is not ``application/json``, this + will return empty object. + + .. versionchanged:: 2.3 + force return json instead of raise a 415 Unsupported Media Type error. + """ + return self.get_json(force=True) + @property def max_content_length(self) -> int | None: # type: ignore """Read-only view of the ``MAX_CONTENT_LENGTH`` config key."""