How can I get a custom header from the client in Tomcat?

427 views Asked by At

When I run the following block of code, I only see some of the request headers in Tomcat. I don't see some of the custom ones I've added.

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    List<String> headerNames = Collections.list((Enumeration<String>) request.getHeaderNames());
    for (String key : headerNames) {
        System.out.println("Key: " + key + " Value: " + request.getHeader(key));
    }
}

Here are the headers I sent:

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Cache-Control: max-age=0
Connection: keep-alive
Cookie: test=test
Host: localhost:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:37.0) Gecko/20100101 Firefox/37.0
CUSTOM_HEADER: test

And here is what the servlet prints out (notice that the CUSTOM_HEADER value isn't there):

Key: host Value: localhost:8080
Key: connection Value: keep-alive
Key: cache-control Value: max-age=0
Key: accept Value: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Key: user-agent Value: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
Key: accept-encoding Value: gzip, deflate, sdch
Key: accept-language Value: en-US,en;q=0.8

Can anyone tell me what I'm doing wrong?

1

There are 1 answers

0
Tony Zampogna On

There was a user error. I just tried it again in the browser I had setup to send the custom headers and it worked.