apisix icon indicating copy to clipboard operation
apisix copied to clipboard

bug: error message in text/html

Open kratosmat opened this issue 8 months ago • 4 comments

Current Behavior

Hi all, I am newbie on apisix, so I hope I am not asking something already discussed.

I noticed that calling some apis via apisix, when there is some error, it send a text/html response, such as 404 or 401 in html format. Now we are using apisix to protect Rest API and we don't expect to have html response.

I think that this shouldn't be the default behaviour, anyway there is a way to configure to have reponses in application/json, or is it a bug?

Thanks a lot, Matteo.

Expected Behavior

No response

Error Logs

No response

Steps to Reproduce

  1. create a route to http bin
  2. call the api with a wrong context via curl or postman
  3. check the response body and response headers

Environment

  • APISIX version (run apisix version): 3.11.0-debian
  • Operating system (run uname -a): kubernetes image debian
  • OpenResty / Nginx version (run openresty -V or nginx -V):
  • etcd version, if relevant (run curl http://127.0.0.1:9090/v1/server_info): etcd:3.5.10-debian-11-r2
  • APISIX Dashboard version, if relevant:
  • Plugin runner version, for issues related to plugin runners:
  • LuaRocks version, for installation issues (run luarocks --version):

kratosmat avatar May 28 '25 18:05 kratosmat

The topic was actually discussed many times on different threads, here are some of them:

  • #7987
  • #8628
  • #11855
  • #12007
  • #12023

There are different ways you can change it:

  • Setting a custom NGiNX configuration which overrides the default response (as explained in #7987).
  • Using Enterprise plugin error-page, from API7.
  • Developing your own custom plugin.

mikyll avatar May 28 '25 19:05 mikyll

Hello Michele, thanks a lot for your response, it was very useful. However, I have implemented the first solution, and it works for some error, but for 404 it doesn't work. Did Someone have a similr experience?

kratosmat avatar May 29 '25 20:05 kratosmat

Can you share your full configuration and test method?

Baoyuantop avatar May 30 '25 01:05 Baoyuantop

Hello, I am using the official helm, so this is the configuration

-- Custom configuration snippet.

configurationSnippet: main: |

httpStart: | more_clear_headers Server;

map $status $status_text {
100 "Continue";
101 "Switching Protocols";
102 "Processing";
103 "Early Hints";

200 "OK";
201 "Created";
202 "Accepted";
203 "Non-Authoritative Information";
204 "No Content";
205 "Reset Content";
206 "Partial Content";
207 "Multi-Status";
208 "Already Reported";
226 "IM Used";

300 "Multiple Choices";
301 "Moved Permanently";
302 "Found";
303 "See Other";
304 "Not Modified";
305 "Use Proxy";
307 "Temporary Redirect";
308 "Permanent Redirect";

400 "Bad Request";
401 "Unauthorized";
402 "Payment Required";
403 "Forbidden";
404 "Not Found";
405 "Method Not Allowed";
406 "Not Acceptable";
407 "Proxy Authentication Required";
408 "Request Timeout";
409 "Conflict";
410 "Gone";
411 "Length Required";
412 "Precondition Failed";
413 "Content Too Large";
414 "URI Too Long";
415 "Unsupported Media Type";
416 "Range Not Satisfiable";
417 "Expectation Failed";
418 "I'm a teapot";
421 "Misdirected Request";
422 "Unprocessable Content";
423 "Locked";
424 "Failed Dependency";
425 "Too Early";
426 "Upgrade Required";
428 "Precondition Required";
429 "Too Many Requests";
431 "Request Header Fields Too Large";
451 "Unavailable For Legal Reasons";
500 "Internal Server Error";
501 "Not Implemented";
502 "Bad Gateway";
503 "Service Unavailable";
504 "Gateway Timeout";
505 "HTTP Version Not Supported";
506 "Variant Also Negotiates";
507 "Insufficient Storage";
508 "Loop Detected";
510 "Not Extended";
511 "Network Authentication Required";
default "Something is wrong";
}
map $http_accept $extension {
default html;
~*application/json json;
}

httpEnd: |

httpSrv: | error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error_$extension;

location @error_json {
  types { } default_type "application/json; charset=utf-8";
echo '{"error_msg": "$status_text"}';
}
location @error_html {
  types { } default_type "text/html; charset=utf-8";
  echo '<html><head><title>$status

$status_text

$status $status_text

'; }

httpAdmin: |

stream: |

To test the behaviour I have used the example in documentation at https://apisix.apache.org/docs/apisix/getting-started/configure-routes/ and postman to call the route.

[image: image.png]

this is the problem [image: image.png]

Furthemore, I have enabled the ingress (not ingress controller) based on haproxy, so I thought this could be the problem, but the response is from apisix and for other errors, such as 401 (I am using openid plugin to authenticate against keycloak) it works, it sends me a json response.

Thanks a lot, Matteo Cusmai

On Fri, May 30, 2025 at 3:39 AM Baoyuan @.***> wrote:

Baoyuantop left a comment (apache/apisix#12264) https://github.com/apache/apisix/issues/12264#issuecomment-2920989949

Can you share your full configuration and test method?

— Reply to this email directly, view it on GitHub https://github.com/apache/apisix/issues/12264#issuecomment-2920989949, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABREXGTYQUPIMNVC64HXJZL3A6ZF3AVCNFSM6AAAAAB6DQ75ECVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDSMRQHE4DSOJUHE . You are receiving this because you authored the thread.Message ID: @.***>

kratosmat avatar May 30 '25 05:05 kratosmat