From 6ca0af82b5edba68b54411d08606a0d93c3d9bdd Mon Sep 17 00:00:00 2001 From: Gavin D'souza Date: Thu, 29 Apr 2021 11:01:57 +0530 Subject: [PATCH] fix: Respond to /api requests as JSON by default If header 'Accept: application/json' isn't set, the failure responses to /api endpoints is HTML. Success responses are of type JSON. (cherry picked from commit fcf63622bc0b557fdbbf4b91c30c638a7d106891) --- frappe/app.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/frappe/app.py b/frappe/app.py index c9e993a853..794d0f18af 100644 --- a/frappe/app.py +++ b/frappe/app.py @@ -201,12 +201,20 @@ def handle_exception(e): response = None http_status_code = getattr(e, "http_status_code", 500) return_as_message = False + accept_header = frappe.get_request_header("Accept") or "" + respond_as_json = ( + frappe.get_request_header('Accept') + and (frappe.local.is_ajax or 'application/json' in accept_header) + or ( + frappe.local.request.path.startswith("/api/") and not accept_header.startswith("text") + ) + ) if frappe.conf.get('developer_mode'): # don't fail silently print(frappe.get_traceback()) - if frappe.get_request_header('Accept') and (frappe.local.is_ajax or 'application/json' in frappe.get_request_header('Accept')): + if respond_as_json: # handle ajax responses first # if the request is ajax, send back the trace or error message response = frappe.utils.response.report_error(http_status_code)