2

How can I override in Tomcat 7 the text of the HttpStatus.

I'm using HttpServletResponse.sendError(401, "Invalid username or Password"), but when I'm looking at the response status in the client it goves 401 Unauthorized.

Is there any way to override it?

Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44
Cristi
  • 190
  • 2
  • 10
  • Possible duplicate of ["How to show user-friendly error page..."](http://stackoverflow.com/questions/2748220/how-to-show-user-friendly-error-page-instead-of-tomcat-log-with-stack-trace-in-b). – dcsohl Dec 19 '13 at 16:20
  • @dcsohl Not a duplicate. This question is about the HTTP header. The other is about custom error pages. – Javier Dec 19 '13 at 17:22

3 Answers3

6

Tomcat no longer supports USE_CUSTOM_STATUS_MSG_IN_HEADER property.

Changelog from 8.5.0:

RFC 7230 states that clients should ignore reason phrases in HTTP/1.1 response messages. Since the reason phrase is optional, Tomcat no longer sends it. As a result the system property org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER is no longer used and has been removed. (markt)

RFC 7230, Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing, June 2014. Section 3.1.2:

The reason-phrase element exists for the sole purpose of providing a textual description associated with the numeric status code, mostly out of deference to earlier Internet application protocols that were more frequently used with interactive text clients. A client SHOULD ignore the reason-phrase content.

Community
  • 1
  • 1
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
4

No - the response codes are set according to RFC 2616. If you want to communicate a message to the user (to the API client) either write it in the body or in a response header

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
  • 3
    True, response codes are specified in the RFC but the "Reason Phrase" (which is what the question is about) can be anything you like. (See section [rfc2616 section 6.1.1](http://tools.ietf.org/html/rfc2616#section-6.1.1) "The reason phrases listed here are only recommendations -- they MAY be replaced by local equivalents without affecting the protocol." – Moose Morals Mar 25 '15 at 12:48
4

Edit catalina.properties and add the property:

org.apache.coyote.USE_CUSTOM_STATUS_MSG_IN_HEADER=true

With that set in my dev environment, then when I do:

response.sendError(HttpServletResponse.SC_BAD_REQUEST, 
                   "A very very very bad request");

I see:

HTTP/1.1 400 A very very very bad request
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=utf-8
Content-Language: en
Content-Length: 1024
Date: Fri, 20 Dec 2013 11:09:54 GMT
Connection: close

Also discussed here and here

Community
  • 1
  • 1
Will Keeling
  • 22,055
  • 4
  • 51
  • 61