-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
have request.get_json() return an error message #2339
Comments
It is documented to return |
Hmm, on second thought, maybe I could transition to |
I'll try it out in 2.1 and see how it goes. |
Never mind, setting |
I could remove |
This works the same for form data, if the content type isn't I think the most we can do is log a warning if the content type is incorrect. |
I think that it's ok to have a difference between form parsing returning silently and JSON parsing failing with a 400 error. As I pointed out, it's much more common for clients to not set a JSON content type. Since that behavior is such a common source of confusion, returning an informative 400 error will make it easier to understand and fix the issue. |
pallets/werkzeug#2339 changed the behavior of Request.get_json() and the Request.json property to raise a BadRequest if Request.json is accessed and the content type is not `application/json`. Argument parsing allows parsing from multiple locations, but if the locations include `json` and the content type is not `application/json`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Handle this case and continue argument parsing.
This feature change caused all API endpoints to return 400 with Connexion 2.13.0 and Flask 2.1.0. Pinning Werkzeug to release 2.0.* resolved it. |
This change breaks Flask-RESTful, which attempts to get both JSON and value arguments from any request while trying to parse arguments (reqparse.py line 125). The call to getattr() results in a call to get_json() in werkzeug, which bombs with the BadRequest exception. There is no way to pass the force option here. Flask-RESTful was leveraging the return of None when called on a non-JSON request. |
pallets/werkzeug#2339 changed the behavior of `Request.get_json()` and the `Request.json` property to raise a `BadRequest` if `Request.get_json()` is called without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`.
pallets/werkzeug#2339 changed the behavior of `Request.get_json()` and the `Request.json` property to raise a `BadRequest` if `Request.get_json()` is called without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`.
pallets/werkzeug#2339 changed the behavior of `Request.get_json()` and the `Request.json` property to raise a `BadRequest` if `Request.get_json()` is called without `silent=True`, or the `Request.json` property is accessed, and the content type is not `"application/json"`. Argument parsing allows parsing from multiple locations, and defaults to `["json", "values"]`, but if the locations include `"json"` and the content type is not `"application/json"`, a `BadRequest` is now raised with Werkzeug >= 2.1.0. Invoking `Request.get_json()` with the `silent=True` parameter now handles the situation where `"json"` is included in the locations, but the content type is not `"application/json"`.
Request.get_json(), (or a new function), should return some sort of error message if the
force
parameter is not used notifying the user that this method only functions if themime
type isapplication/json
unless theforce
option is used.Alternatively, current usage can look like this:
In most scenarios there would be a failure at some stage or some form of custom handling for the current
None
behavior. Given that the function expects that application/json is in use it seems reasonable to enforce that expectation at some level.To be totally fair, this would be a breaking change to anyone relying on the current
None
behavior so it would also be great if some sort ofget_json_strict()
existed with similar functionality. The end goal here is to just have some default visibility around this particular behavior ofget_json()
.This would affect
werkzeug/src/werkzeug/wrappers/request.py
Line 540 in 8292df2
The text was updated successfully, but these errors were encountered: