Open jbmoorhouse opened 2 years ago
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. :)
This middleware adds Content-Type: application/json
header to the POST
, PUT
, PATCH
requests if there is not Content-Type
header set in the request
class JSONContentTypeMiddleware:
def __init__(self, wsgi_app: Any):
self.wsgi_app = wsgi_app
def __call__(self, environ: dict[str, Any], start_response: Any) -> Any:
if environ["REQUEST_METHOD"] in ("POST", "PUT", "PATCH"):
content_type = environ.get("CONTENT_TYPE")
if not content_type:
environ["CONTENT_TYPE"] = "application/json"
return self.wsgi_app(environ, start_response)
And then
flask_app = Flask(__name__)
flask_app.wsgi_app = JSONContentTypeMiddleware(flask_app.wsgi_app)
Code
Repro Steps (if applicable)
foo
query param set astrue
/false
Expected Behavior
I would expect this to return a just and empty list (i.e. not really do anything)
Actual Behavior
Error Messages/Stack Trace
parse_args
raises this error inreqparse.py
Environment