flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Werkzeug 2.1.2 Did not attempt to load JSON data

Open jbmoorhouse opened this issue 2 years ago • 6 comments

Code

from flask import Flask
from flask_restx import Api, Resource
from werkzeug.middleware.proxy_fix import ProxyFix

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
api = Api(
    app,
    version="1.0",
    title="Foo",
    description="A simple foo",
)

ns = api.namespace("Foo", description="bar")

@ns.route("/")
class Foo(Resource):
    """Shows a list of all todos, and lets you POST to add new tasks"""

    get_parser = api.parser()
    get_parser.add_argument(
        "foo",
        type=bool,
        default=True,
    )

    @ns.doc("foo", parser=get_parser)
    def get(self):
        """List all tasks"""
        self.get_parser.parse_args()
        return "foo"


if __name__ == "__main__":
    app.run(debug=True)

Repro Steps (if applicable)

  1. Run the app
  2. Then try out the only endpoint with the foo query param set as true/false
  3. Broken!

Expected Behavior

I would expect this to return a just and empty list (i.e. not really do anything)

Actual Behavior

{
  "message": "Did not attempt to load JSON data because the request Content-Type was not 'application/json'."
}

Error Messages/Stack Trace

parse_args raises this error in reqparse.py

Environment

  • Python version 3.8
  • Flask version 2.1.2
  • Flask-RESTX version 0.5.1
  • Werkzeug 2.1.2

jbmoorhouse avatar Jul 14 '22 15:07 jbmoorhouse

Bit of nasty temporary workaround, but it works for now

from flask import Flask as Flask_, Request as Request_

class Request(Request_):
    def get_json(self, *args, **kwargs):
        kwargs.update(silent=True)
        return super().get_json(*args, **kwargs)


class Flask(Flask_):
    request_class = Request

Then create your flask app using this new Flask object

jbmoorhouse avatar Jul 15 '22 14:07 jbmoorhouse

Think this is a duplicate of https://github.com/python-restx/flask-restx/issues/422 ?

peter-doggart avatar Jul 19 '22 13:07 peter-doggart

Was about to comment, but then your comment showed up, @peter-doggart – indeed, it is a duplicate.

This also occured in https://stackoverflow.com/questions/72157708/flask-restx-request-parser-returns-400-bad-request.

The failing line is https://github.com/python-restx/flask-restx/blob/88497ced96674916403fa7829de693eaa3485a08/flask_restx/reqparse.py#L149 And the reason is that werkzeug raises if the content type does not match since 2.1: https://github.com/pallets/werkzeug/blob/acb1b04ef2e0ff269dc5313462c341dfaacb1b5b/src/werkzeug/wrappers/request.py#L537-L538

shoeffner avatar Jul 19 '22 13:07 shoeffner

Is there any update on a fix for this issue?

pype-leila avatar Jul 25 '22 16:07 pype-leila

I fixed flask-restx/flask_restx/reqparse.py in my restx-monkey patches. I hope it will help you, feedback is welcome. :)

Ryu-CZ avatar Aug 31 '22 14:08 Ryu-CZ