Print request and response headers using HttpServletRequest and HTTPServletResponse

Often in JAVA we need to troubleshoot the incoming requests, outgoing requests , incoming responses and outgoing responses. This is a typical situation for cloud native developments where an application or micro service is developed. One of the main troubleshooting steps is to print the request and response headers. Today I am going to show how to print those request and response headers for a JAVA application.


 

Contents

  1. Printing request headers using HttpServletRequest object

  2. Printing response headers using HttpServletResponse object

 

Printing request headers using HttpServletRequest object

After you have received a request you can use this method to print all headers.

public void printRequestHeaders(HttpServletRequest req) {
  Enumeration names = req.getHeaderNames();
  if(names == null) {
    return;
  }
    while(names.hasMoreElements()) {
      String name = (String) names.nextElement();
       Enumeration values = req.getHerders(name);
       if(values != null) {
         while(values.hasMoreElements()) {
             String value = (String) values.nextElement();
             System.out.println(name + " : " + value );
         }
       }
    }
}

Printing response headers using HttpServletResponse object

After you have populated all headers in response object and populated response body you can print

public void printResponseHeaders(HttpServletResponse req) {
  Collection<String> names = res.getHeaderNames();
  if(names == null) {
    return;
  }
  Iterator namesIterator = names.iterator();
  while(namesIterator.hasNext()) {
   String name = (String)  namesIterator.next();
    Collection<String> values = res.getHeaders(name);
    if(values != null) {
       Iterator valuesIterator = values.iterator();
       while(valuesIterator.hasNext()) {
            String value = (String) valuesIterator.next();
            System.out.println(name + " : " + value );         
       }
    }
  }
}

This brief post shows how we can print request and response headers in JAVA.


15 views0 comments

Recent Posts

See All