Logo
Explore Help
Sign In
darkhelm/notifications-api
1
0
Fork 0
You've already forked notifications-api
mirror of https://github.com/GSA/notifications-api.git synced 2026-03-03 08:52:22 -05:00
Code Issues Packages Projects Releases Wiki Activity
Files
commit-early-for-save-api-email
notifications-api/app/v2/utils.py

14 lines
373 B
Python
Raw Permalink Normal View History

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.
2019-11-21 15:21:18 +00:00
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 Copy Permalink
Powered by Gitea Version: 1.25.0 Page: 1818ms Template: 10ms
English
Bahasa Indonesia Deutsch English Español Français Gaeilge Italiano Latviešu Magyar nyelv Nederlands Polski Português de Portugal Português do Brasil Suomi Svenska Türkçe Čeština Ελληνικά Български Русский Українська فارسی മലയാളം 日本語 简体中文 繁體中文(台灣) 繁體中文(香港) 한국어
Licenses API