You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when some multipart/form-data request is parsed via a reqparse.RequestParser(), the parsing fails with
werkzeug.exceptions.BadRequest: 400 Bad Request: Did not attempt to load JSON data because the request Content-Type was not 'application/json'.
To replicate create some endpoint, which parses the request with a reqparse.RequestParser and send the requested data as multipart/form-data for example with curl.
With flask version 2.0.2 (and a corresponding older werkzeug version m(i.e. <2.1.1) ), this worked as expected, i.e. the request gets parsed correctly, with flask version 2.1.1 and werkzeug 2.1.1 (or newer) it fails.
This happens, because with werkzeug version 2.1.1, werkzeug the behaviour of request.get_json() was changes:
"Request.get_json() will raise a 400 BadRequest error if the Content-Type header is not application/json. This makes a very common source of confusion more visible. #2339"
requparse.Argument now loads its location as default with
location=('json', 'values',)
in line 77 of the __init__ function and then iterates over self.location (in line 125f):
for l in self.location:
value = getattr(request, l, None)
which access request.json (first) and then fails due to the new exception in/from werkzeug.
Capturing that exception or accessing json only if the application type matches json would prevent that error and would allow to continue to use the reqparse with application types not being json.
The text was updated successfully, but these errors were encountered:
Thank you @davidism for the helpful reply! Your workaround does work for me! And I am sorry, I did not find the duplicate issues. Thanks again for pointing it out.
when some
multipart/form-data
request is parsed via areqparse.RequestParser()
, the parsing fails withTo replicate create some endpoint, which parses the request with a
reqparse.RequestParser
and send the requested data asmultipart/form-data
for example withcurl
.With flask version 2.0.2 (and a corresponding older werkzeug version m(i.e. <2.1.1) ), this worked as expected, i.e. the request gets parsed correctly, with flask version 2.1.1 and werkzeug 2.1.1 (or newer) it fails.
This happens, because with werkzeug version 2.1.1, werkzeug the behaviour of request.get_json() was changes:
"Request.get_json() will raise a 400 BadRequest error if the Content-Type header is not application/json. This makes a very common source of confusion more visible. #2339"
(c.f. https://werkzeug.palletsprojects.com/en/2.1.x/changes/ or pallets/werkzeug#2339)
requparse.Argument
now loads its location as default within line 77 of the
__init__
function and then iterates overself.location
(in line 125f):which access
request.json
(first) and then fails due to the new exception in/from werkzeug.Capturing that exception or accessing json only if the application type matches json would prevent that error and would allow to continue to use the reqparse with application types not being json.
The text was updated successfully, but these errors were encountered: