HttpServletRequest and getHeader(): How to handle case insensitive headers properly?
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) {
Malax
Updated on June 14, 2022Comments
-
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 over 14 yearsActually, its Jetty 5. I'll investigate this issue, thank for pointing me to the Java EE documentation... ;-)
-
matt b over 14 yearsI think Jetty might be up to version 6 (or 7?), you might want to take a look at upgrading.
-
bmauter over 10 yearsSorry to resurrect an old thread, but I just wanted to note that Tomcat 7.0.39 also returns case sensitive headers.
-
Jon Skeet over 10 years@bmauter: Do you mean that if you request
CONTENT-TYPE
it won't give you the value forContent-Type
? That sounds broken... -
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 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 over 10 years@JonSkeet Nevermind, it looks like I am an idiot. Sorry about that.
-
Raul Luna over 6 yearsActually, Tomcat 8.0.9 is doing this way: the headers are all lowercase. :((
-
abdel about 6 yearssame 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.