From 026f8cef61026267d6d9b0a2a34921c0ecd9085a Mon Sep 17 00:00:00 2001 From: "Stacy W. Smith" Date: Mon, 28 Mar 2022 17:48:36 -0600 Subject: [PATCH] Handle Werkzeug 2.1.0 change to `Request.get_json()`. pallets/werkzeug#2339 changed the behavior of `Request.get_json()` and the `Request.json` property to raise a `BadRequest` if `Request.get_json()` is called without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`. --- flask_restx/reqparse.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flask_restx/reqparse.py b/flask_restx/reqparse.py index 63260660..8bb4db13 100644 --- a/flask_restx/reqparse.py +++ b/flask_restx/reqparse.py @@ -146,7 +146,10 @@ def source(self, request): else: values = MultiDict() for l in self.location: - value = getattr(request, l, None) + if l in {"json", "get_json"}: + value = request.get_json(silent=True) + else: + value = getattr(request, l, None) if callable(value): value = value() if value is not None: