mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 23:41:17 -05:00
Check that the request payload data is valid json.
By adding `force=True` to request.get_json() the mime type is ignore. If the data is not valid json the method will return a `BadRequestError` we catch that and throw our own error with a clear error message "Invalid JSON supplied in POST data". If the json is valid return the json data or an empty dict if None is passed in. This PR improves the error messages if the json is invalid, previously, the error message was "None object type" message which is not very helpful.
This commit is contained in:
13
app/v2/utils.py
Normal file
13
app/v2/utils.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from flask import request
|
||||
|
||||
from app.v2.errors import BadRequestError
|
||||
from werkzeug.exceptions import BadRequest
|
||||
|
||||
|
||||
def get_valid_json():
|
||||
try:
|
||||
request_json = request.get_json(force=True)
|
||||
except BadRequest:
|
||||
raise BadRequestError(message="Invalid JSON supplied in POST data",
|
||||
status_code=400)
|
||||
return request_json or {}
|
||||
Reference in New Issue
Block a user