HttpServletRequest and getHeader(): How to handle case insensitive headers properly?

19,890

Solution 1

Which servlet container are you using? The docs for getHeader(String) state:

The header name is case insensitive.

so it sounds like a bug in the container you're using.

Solution 2

tomcat 8.0.24 impl of getHeader delegates to 'org.apache.tomcat.util.http.MimeHeaders' which eventually calls this method below which in turn does case insensitive check

313  public MessageBytes getValue(String name) {
Share:
19,890
Malax
Author by

Malax

Updated on June 14, 2022

Comments

  • Malax
    Malax almost 2 years

    Since I discovered that HTTP headers are case-insensive according to the RFC, i was wondering how I can access HTTP headers in a case-insensitive way with Servlets. There is a #getHeader(String) method to obtain a header but it turned out that this method treats the header fields case sensitive.

    Is there a "case insensitive" way to get header fields ? Or do i have to iterate over all the header fields to find the header field I was looking for ?

  • Malax
    Malax over 14 years
    Actually, its Jetty 5. I'll investigate this issue, thank for pointing me to the Java EE documentation... ;-)
  • matt b
    matt b over 14 years
    I think Jetty might be up to version 6 (or 7?), you might want to take a look at upgrading.
  • bmauter
    bmauter over 10 years
    Sorry to resurrect an old thread, but I just wanted to note that Tomcat 7.0.39 also returns case sensitive headers.
  • Jon Skeet
    Jon Skeet over 10 years
    @bmauter: Do you mean that if you request CONTENT-TYPE it won't give you the value for Content-Type? That sounds broken...
  • bmauter
    bmauter over 10 years
    @JonSkeet Correct. I asked for "Authorization" and got nothing when iOS sent "authorization". I can reproduce this at will.
  • Jon Skeet
    Jon Skeet over 10 years
    @bmauter: Odd. I'd file a bug against Tomcat (or see if there's an existing one). It's clearly violating the spec, judging by your description.
  • bmauter
    bmauter over 10 years
    @JonSkeet Nevermind, it looks like I am an idiot. Sorry about that.
  • Raul Luna
    Raul Luna over 6 years
    Actually, Tomcat 8.0.9 is doing this way: the headers are all lowercase. :((
  • abdel
    abdel about 6 years
    same as @JonSkeet , getHeader("Authorization") is failing to recognize header "authorization", and this is on tomcat8. When i change the actual header to "Authorization" then it works.