How do I print the content of httprequest request?

后端 未结 6 452
名媛妹妹
名媛妹妹 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:32

    Rewrite @Juned Ahsan solution via stream in one line (headers are treated the same way):

    public static String printRequest(HttpServletRequest req) {
        String params = StreamSupport.stream(
                ((Iterable) () -> req.getParameterNames().asIterator()).spliterator(), false)
                .map(pName -> pName + '=' + req.getParameter(pName))
                .collect(Collectors.joining("&"));
        return req.getRequestURI() + '?' + params;
    }
    

    See also how to convert an iterator to a stream solution.

提交回复
热议问题