Skip to content

Commit

Permalink
Handle Werkzeug 2.1.0 change to Request.get_json().
Browse files Browse the repository at this point in the history
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"`.
  • Loading branch information
Stacy W. Smith authored and stacywsmith committed Apr 1, 2022
1 parent 88497ce commit 026f8ce
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion flask_restx/reqparse.py
Expand Up @@ -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:
Expand Down

0 comments on commit 026f8ce

Please sign in to comment.