JSON for a Custom Error Page
Heyo, I was looking around and I couldn’t find any way to use a JSON message for an error page. Ideally, we would return a JSON error parsable by the API Client, and not any HTML. Is this possible? It seems more of a use case for Snippets but I would like to check if a native error integration is possible for things like rate limits and blocked requests. Thanks!
3 Replies
Custom Error Responses can return JSON for matching requests: https://developers.cloudflare.com/rules/custom-errors/create-api/#custom-json-response-for-all-5xx-errors
Cloudflare Docs
Create custom errors via API · Cloudflare Rules docs
To configure custom errors via API:
if you are using WAF / Rate Limiting, same can be done via custom response there: https://developers.cloudflare.com/waf/custom-rules/create-dashboard/#configure-a-custom-response-for-blocked-requests
Cloudflare Docs
Create a custom rule in the dashboard · Cloudflare Web Application...
When you select the Block action in a rule you can optionally define a custom response.
one thing to keep in mind is that Snippets is a request phase that runs after WAF, so unless your call that is blocked by WAF is a subrequest from Snippets, you won't be able to override the error coming from WAF using Snippets: https://developers.cloudflare.com/ruleset-engine/reference/phases-list/
Cloudflare Docs
Phases list · Cloudflare Ruleset Engine docs
The following tables list the phases of Cloudflare products powered by the Ruleset Engine, in the order those phases are executed. Some products such as the Cloudflare Web Application Firewall have more than one associated phase.