Why HTTP 501 error of type JSON presented to UI as text/html instead of application/json?

I have front-end Angular running on Apache with back-end Spring REST API on Tomcat.

I wanted to throw 501 error in some error case and want that response to be presented as JSON to UI (Content-Type as "application/json"). This works fine in my local as expected with Angular is able to interpret the JSON response but when it comes to some common QA environment it fails because the 501 error presented to UI as text/html which Angular can’t digest.

Difference between my local and QA is, Angular is running on node whereas QA uses Apache. Is the apache who is converting JSON response with 501 http code to html type?

the response i get on my browser is below.

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Not Implemented</title>
</head><body>
<h1>Not Implemented</h1>
<p>GET to /test/customer/getCustomer not supported.<br />
</p>
</body></html>

Answer

Finally i was able to find what is causing Apache to replace all non 2XX responses with its own error document. All you need to do is, comment or set off ProxyErrorOverride. With this Apache will proxy pass error response as is from backend server to UI without loosing content, example it sends error response of type Json as it is without loosing headers.

ProxyErrorOverride Off

Hope this helps someone.

Attribution
Source : Link , Question Author : Anil Konduru , Answer Author : Community

Leave a Comment