title: Read HTTP POST from mobile in java
body: <p>I have iOS Swift code, which sends a POST request to server. If i send this code directly to apple server i get response back with proper data. But when i send this to my server, server could not get the body of the HTTP POST. </p>
  
  <p>I have no idea whether this issue is related to client side or server side.</p>
  
  <p>Here is the Swift code.</p>
  
  <pre><code>func validateReceipt(completion : (status : Bool) -&gt; ()) {
  
          let receiptUrl = NSBundle.mainBundle().appStoreReceiptURL!
  
          if NSFileManager.defaultManager().fileExistsAtPath(receiptUrl.path!)
          {
              if let receipt : NSData = NSData(contentsOfURL: receiptUrl)
              {
                  let receiptdata: NSString = receipt.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.EncodingEndLineWithLineFeed)
  
                  let dict = ["receipt-data" : receiptdata]
  
                  let jsonData = try! NSJSONSerialization.dataWithJSONObject(dict, options: NSJSONWritingOptions(rawValue: 0))
  
                  let request = NSMutableURLRequest(URL: NSURL(string: ReceiptURL.MAIN_SERVER.rawValue)!)
  
                  let session = NSURLSession.sharedSession()
                  request.HTTPMethod = "POST"
                  request.HTTPBody = jsonData
  
                  let task = session.dataTaskWithRequest(request, completionHandler: { data, response, error in
  
                      if let dataR = data
                      {
                          self.handleData(dataR, completion: { status in
  
                              completion(status: status)
                          })
                      }
                  })
  
                  task.resume()
              }
              else
              {
                  completion(status: false)
              }
          }
          else
          {
              completion(status: false)
          }
      }
  </code></pre>
  
  <p>and here is my Java code in server side, there are two Java classes which take care of this</p>
  
  <pre><code>MyRequestWrapper.Java
  
  package webservice;
  
  import java.io.BufferedReader;
  import java.io.ByteArrayInputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  
  import javax.servlet.ServletInputStream;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletRequestWrapper;
  
  public class MyRequestWrapper extends HttpServletRequestWrapper {
       private final String body;
       public MyRequestWrapper(HttpServletRequest request) throws IOException {
         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[100000];
             int bytesRead = -1;
             while ((bytesRead = bufferedReader.read(charBuffer)) &gt; 0) {
               stringBuilder.append(charBuffer, 0, bytesRead);
             }
           } else {
             stringBuilder.append("");
           }
         } catch (IOException ex) {
             throw ex;
         } finally {
           if (bufferedReader != null) {
             try {
               bufferedReader.close();
             } catch (IOException ex) {
               throw ex;
             }
           }
         }
         body = stringBuilder.toString();
       }
  
       @Override
       public ServletInputStream getInputStream() throws IOException {
         final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
         ServletInputStream servletInputStream = new ServletInputStream() {
           public int read() throws IOException {
             return byteArrayInputStream.read();
           }
         };
         return servletInputStream;
       }
  
       @Override
       public BufferedReader getReader() throws IOException {
         return new BufferedReader(new InputStreamReader(this.getInputStream()));
       }
  
       public String getBody() {
         return this.body;
       }
      }
  </code></pre>
  
  <p>And here is the another class.</p>
  
  <pre><code>GetResult.Java
  
  package webservice;
  
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.PrintWriter;
  
  import javax.servlet.FilterChain;
  import javax.servlet.ServletException;
  import javax.servlet.ServletRequest;
  import javax.servlet.ServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.Path;
  import javax.ws.rs.Produces;
  import javax.ws.rs.core.Context;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.Response;
  
  import org.apache.log4j.Logger;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  @Path("/service")
  public class GetResult {
      static Logger logger = Logger.getLogger(GetResult.class);
      // @Produces("application/json; charset=UTF-8")
      //@Produces("text/plain")
      @POST
      @Produces (MediaType.APPLICATION_JSON)
      public Response inapp(@Context HttpServletRequest request,
              @Context HttpServletResponse response) throws Exception {
  
          System.out.println("response===" + response);
          System.out.println("Request-Header===" + request.getHeader("receipt-data"));
          System.out.println("Request===" + request.getParameter("receipt-data"));
          // System.out.println("Request==="+request.getReader());
          // reader(request,response);
          // getBody(request);
          doFilter(request,response);
          String result = "";
          result = " " /* jsonObject */;
          return Response.status(200).entity(result).build();
  
      }
  </code></pre>
  
  <p>We could get the client IP and client Port from this request but unable to get the body. In production also we could not get the body. Some Java developers told me that you cant directly get the raw bytes in Java, i don't know about this.</p>
  
  <p>Somebody please take a look at this, and tell me what i am doing wrong.</p>