Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reqparse fails on multipart/form-data due to change in werkzeug.request #4552

Closed
dheldt opened this issue Apr 26, 2022 · 2 comments
Closed

Comments

@dheldt
Copy link

dheldt commented Apr 26, 2022

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"

(c.f. https://werkzeug.palletsprojects.com/en/2.1.x/changes/ or pallets/werkzeug#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.

@davidism
Copy link
Member

Duplicate of flask-restful/flask-restful#936 and python-restx/flask-restx#422. Note that both have associated PRs, but neither appear to have been maintained in some time.

You can do the following in your own project to disable errors on bad content type.

from flask.wrappers import Request

class AnyJsonRequest(Request):
    def on_json_loading_failed(self, e):
        if e is not None:
            return super().on_json_loading_failed(e)

app.request_class = AnyJsonRequest

@dheldt
Copy link
Author

dheldt commented Apr 27, 2022

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants