Error response Bad gateway
Hi everyone,
I’m running my API through Cloudflare (Free plan) and everything works fine for 200 responses, which come back as JSON. However, whenever my origin returns a 4xx or 5xx, Cloudflare serves its standard HTML error page (e.g. 502 Bad Gateway) instead of passing through the JSON payload I expect.
What I’ve tried so far:
Disabled HTTP/3 (with QUIC) under Speed → Optimization → Protocol Optimization — this temporarily fixed it, but after two months the HTML errors returned.
Questions:
Is there a Free-plan setting or page rule I can use to bypass Cloudflare’s HTML error pages and preserve my JSON error responses?
Are there any known issues or workarounds around error handling on Cloudflare’s edge?
Any guidance would be greatly appreciated,
thanks in advance!
4 Replies
Support AI
According to the Cloudflare documentation, you can use Custom Error Rules to return custom content to your website visitors in case of HTTP errors returned by an origin server or by a Cloudflare product. However, Custom Error Rules are only available to paid plans, not the Free plan.
As you are on the Free plan, you won't be able to use Custom Error Rules to bypass Cloudflare's HTML error pages and preserve your JSON error responses.
Regarding the issue you experienced with disabling HTTP/3 (with QUIC) under Speed → Optimization → Protocol Optimization, it's possible that this was a temporary workaround, but it's not a reliable solution.
Unfortunately, there isn't a known issue or workaround around error handling on Cloudflare's edge that would allow you to preserve JSON error responses on the Free plan.
You may want to consider upgrading to a paid plan to access Custom Error Rules and other features that can help you customize error handling.
Source: https://developers.cloudflare.com/rules/custom-errors/
Sources
This is an AI generated response, it may not be 100% accurate. Please validate the output. You can ask followups by replying to this message or mentioning me.
could someone please help me
Do you have an example of a response that Cloudflare turns into a 502 (especially headers like content-type etc)?
I've just seen another topic where JSON responses suddenly turned into 502s, so it might be related.
Hey, I triggered the failing login request again and here’s what I got from Postman:
Status Code: 502 Bad Gateway
Response Headers:
Content-Type: text/html; charset=UTF-8
Server: cloudflare
CF-RAY: [redacted]
Response Body:
It’s a full HTML page from Cloudflare with the title my domain name | 502: Bad gateway, not a JSON response.
So the API request fails only on 400/500 type responses and returns HTML instead of JSON.
On success (200), the JSON is returned as expected.
Let me know what else you’d like me to check or test.
I understand that 502/504 should normally not happen from the login endpoint.
But I intentionally entered a wrong email and password to trigger the error response and show how it behaves when something goes wrong.
In Node.js, the backend is returning this JSON:
{
"message": "Invalid email and / or password",
"error": "Bad Gateway",
"statusCode": 502
}
So the error is being handled in JSON format at the backend.
However, when the request goes through Cloudflare, it doesn’t return this JSON. Instead, it returns a Cloudflare-generated HTML page with a 502 Bad Gateway error.
So it seems like Cloudflare is intercepting the backend’s JSON error and replacing it with its own HTML.
Let me know if you want me to share anything else to help debug this.
I see
I just wanted to ask:
Is there any recommended way to make sure that even error responses like this (e.g., 502) can be returned in JSON instead of HTML?
The reason I’m asking is because on the frontend side, we're expecting consistent JSON formatting, even in failure cases and the HTML response breaks the app flow.
If it’s not possible through Cloudflare.