top of page

Print request and response headers using HttpRequest and HttpResponse

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.


Printing request headers using HttpRequest object

import org.apache.http.HttpRequest;
. . . 
 
public void printReqHeaders(HttpRequest req) {
  Header[] headers = req.getAllHeaders();
  if(headers == null) {
   return;
  }
  for(int i = 0; i< headers.length; i++) {
    String name = headers[i].getName();
    String value = headers[i].getValue();
    System.out.println(name + " : " + value);
  }
}

Printing response headers using HttpResponse object


import org.apache.http.HttpResponse;
. . . 
 
public void printResHeaders(HttpResponse res) {
  Header[] headers = res.getAllHeaders();
  if(headers == null) {
   return;
  }
  for(int i = 0; i< headers.length; i++) {
    String name = headers[i].getName();
    String value = headers[i].getValue();
    System.out.println(name + " : " + value);
  }
}

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



Recent Posts

See All
bottom of page