How do I print the content of httprequest request?

后端 未结 6 456
名媛妹妹
名媛妹妹 2021-01-31 15:04

I\'ve got a bug involving httprequest, which happens sometimes, so I\'d like to log HttpGet and HttpPost request\'s content when that happens.

So, let\'s say, I create H

6条回答
  •  春和景丽
    2021-01-31 15:39

    You can print the request type using:

    request.getMethod();
    

    You can print all the headers as mentioned here:

    Enumeration headerNames = request.getHeaderNames();
    while(headerNames.hasMoreElements()) {
      String headerName = headerNames.nextElement();
      System.out.println("Header Name - " + headerName + ", Value - " + request.getHeader(headerName));
    }
    

    To print all the request params, use this:

    Enumeration params = request.getParameterNames(); 
    while(params.hasMoreElements()){
     String paramName = params.nextElement();
     System.out.println("Parameter Name - "+paramName+", Value - "+request.getParameter(paramName));
    }
    

    request is the instance of HttpServletRequest

    You can beautify the outputs as you desire.

提交回复
热议问题