Skip to content

Instantly share code, notes, and snippets.

@felipebn
Created May 13, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipebn/79f10693277da6ef98f622d0910dfec7 to your computer and use it in GitHub Desktop.
Save felipebn/79f10693277da6ef98f622d0910dfec7 to your computer and use it in GitHub Desktop.
Prints HttpServletRequest headers and cookies
private String printRequestDebug(HttpServletRequest request) {
final String divider = "---------------------------------------------------------------------------------------\n";
StringBuilder sb = new StringBuilder();
sb.append(divider);
Enumeration<String> headers = request.getHeaderNames();
while (headers.hasMoreElements()) {
String headerName = headers.nextElement();
sb.append(String.format("Header[%s]: '%s'\n", headerName, enumerationAsList(request.getHeaders(headerName))));
}
sb.append(divider);
sb.append("\n\n");
sb.append(divider);
Cookie[] cookies = request.getCookies();
if(cookies == null) {
sb.append("No cookies in the request!\n");
}else {
for (Cookie cookie : cookies) {
sb.append(String.format("Cookie[%s]@'%s': '%s'\n", cookie.getName(), cookie.getDomain(), cookie.getValue()));
}
}
sb.append(divider);
return sb.toString();
}
private List<String> enumerationAsList(Enumeration<String> e){
List<String> list = new ArrayList<>();
while(e.hasMoreElements()) list.add(e.nextElement());
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment