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

BadRequest at hasattr(request, 'json') #4504

Closed
hr-sales opened this issue Mar 29, 2022 · 2 comments
Closed

BadRequest at hasattr(request, 'json') #4504

hr-sales opened this issue Mar 29, 2022 · 2 comments

Comments

@hr-sales
Copy link

Hi guys, yesterday i notice that getting or verifying 'json' in request object, an exception (BadRequest) raise

For getattr(request, 'json') or even hasattr(request, 'json') I got:

{BadRequest}400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

Someone can help me to understand? Thanks

Environment:

  • Python version: 3.7, 3.9
  • Flask version: Latest (2.1.0)
@davidism
Copy link
Member

hasattr is actually just a getattr that returns True or False based on if an AttributeError is raised. So when you do hasattr, you're still executing request.json, which is a property that executes request.get_json.

In pallets/werkzeug#2355, accessing json now raises a 400 error instead of returning None if the request is not well formed. You can use silent=True to make it return None instead, or you can use request.is_json instead to check if the request has a JSON content type before trying to decode the data.

@hr-sales
Copy link
Author

Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 13, 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