title: Read http request without losing data in it
body: <p><strong><em>Is there any way to read inputStream of a request without losing the data in it.</em></strong></p>
  
  <hr>
  
  <p>I am trying to take a raw copy of my request into string before processing it. But once I read the inputstream from request, the inputstream is changing to null so, I can't get Parameter from my request later. I tried using <code>CustomHttpServletRequestWrapper</code> but it did not work. Below is the snippet of my code.</p>
  
  <p>The <code>InboundHandler</code> is my handler class which has the <code>processRequest(HttpServletRequest request, HttpServletResponse response)</code> method that is invoked from my servlet class.</p>
  
  <pre><code>public class InboundHandler {
  
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
    try {
     CustomHttpServletRequestWrapper requestWrapper = new CustomHttpServletRequestWrapper(request);
     String body = getRequestBody(requestWrapper.getInputStream());
  
     String from = request.getParameter("from"); //which I'm getting null here
     // I also tried using 
     // String from = requestWrapper.getParameter("from"); // Even this did not work
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  
   private String getRequestBody(InputStream inputStream) {
  
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;
    try {
     if (inputStream != null) {
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      char[] charBuffer = new char[128];
      int bytesRead = -1;
      while ((bytesRead = bufferedReader.read(charBuffer)) != -1) {
       stringBuilder.append(charBuffer, 0, bytesRead);
      }
     }
    } catch (IOException ex) {
     ex.printStackTrace();
     //throw new AuthenticationException("Error reading the request payload", ex);
    } finally {
     if (bufferedReader != null) {
      try {
       bufferedReader.close();
      } catch (IOException iox) {
       // ignore
      }
     }
    }
  
    return stringBuilder.toString();
   }
  
   public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
  
    private final String body;
  
    public CustomHttpServletRequestWrapper(HttpServletRequest request) {
     super(request);
     StringBuilder stringBuilder = new StringBuilder();
     BufferedReader bufferedReader = null;
  
     try {
      InputStream inputStream = request.getInputStream();
  
      if (inputStream != null) {
       bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  
       char[] charBuffer = new char[128];
       int bytesRead = -1;
  
       while ((bytesRead = bufferedReader.read(charBuffer)) &gt; 0) {
        stringBuilder.append(charBuffer, 0, bytesRead);
       }
      } else {
       stringBuilder.append("");
      }
     } catch (IOException ex) {
      ex.printStackTrace();
      //log.error("Error reading the request body...");  
     } finally {
      if (bufferedReader != null) {
       try {
        bufferedReader.close();
       } catch (IOException ex) {
        ex.printStackTrace();
        //log("Error closing bufferedReader...");  
       }
      }
     }
  
     body = stringBuilder.toString();
    }
  
    @Override
    public ServletInputStream getInputStream() throws IOException {
  
     final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
  
     ServletInputStream inputStream = new ServletInputStream() {
      public int read() throws IOException {
       return byteArrayInputStream.read();
      }
     };
  
     return inputStream;
    }
   }
  }
  </code></pre>