flask-restx
flask-restx copied to clipboard
Werkzeug 2.1.2 Did not attempt to load JSON data
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)
- Run the app
- Then try out the only endpoint with the
foo
query param set astrue
/false
- 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
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
Think this is a duplicate of https://github.com/python-restx/flask-restx/issues/422 ?
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
Is there any update on a fix for this issue?
I fixed flask-restx/flask_restx/reqparse.py in my restx-monkey patches. I hope it will help you, feedback is welcome. :)