I have implemented a Filter, in which I want to read the content of request first for some checks and then I would like to go on.
But the problem is, that in the fol
ContentCachingRequestWrapper
doesnt work that way and has some limitations. Only POST request and content type should be application/x-www-form-urlencoded
as far as I remember. If this fits for you, here's what you should do:
final HttpServletRequest req = (HttpServletRequest) request;
HttpServletRequest servletRequest = new ContentCachingRequestWrapper(req);
servletRequest.getParameterMap(); // needed for caching!!
String read = ByteSource.wrap(servletRequest.getContentAsByteArray())
.asCharSource(StandardCharsets.UTF_8).read(); // Please note that we're not touching input stream!!
Hope this helps.
Please try the two proposed solutions mentioned below:
1. HttpServletRequestWrapper servletRequest = new ContentCachingRequestWrapper(req);
OR
2. ContentCachingRequestWrapper servletRequest = new ContentCachingRequestWrapper(req);
Instead of
HttpServletRequest servletRequest = new ContentCachingRequestWrapper(req);
As you can check here that ContentCachingRequestWrapper
class extends HttpServletRequestWrapper
which extends ServletRequestWrapper
and implements HttpServletRequest
.
So here by performing upcasting, you are may be facing this issue. Please check and let me know if this is not the case, then I will debug it further.