0

I have created the Interceptor where i am trying to read the request body .But i keep getting the error

getInputStream() has been called for this request

How to solve it in postHandle ?I can do the same without any error by ovver riding preHandle but i need it in postHandle . public class LoggerInterceptor implements HandlerInterceptor {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {

        try {
            request.getReader();
            String s = IOUtils.toString(request.getReader());

            System.out.println(" post ---- " + s);//i want to log into DB
        } catch (Exception e) {

            System.out.println(" eror " + e);
        }
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }
Pale Blue Dot
  • 511
  • 2
  • 13
  • 33
  • 2
    You cannot do that as, as you already noticed, can only read the body once. So this will fail. Use a `Filter` and wrap it in a request wrapper that enables reading the body multiple times. – M. Deinum Apr 29 '21 at 10:26
  • [Here](https://www.baeldung.com/spring-reading-httpservletrequest-multiple-times) some useful information for you or [this](https://stackoverflow.com/questions/34804205/how-can-i-read-request-body-multiple-times-in-spring-handlermethodargumentresol) – Grigorii Riabov Apr 29 '21 at 10:54

0 Answers0