title: How to get parameter from a PUT request?
body: <p>I am trying to send a JSON array from browser to a Java Servlet on PUT method.</p>
  
  <p>My ajax request:</p>
  
  <pre><code> $.ajax({
    url : "./manageShoppingCart",
    data : {
      jsonArray : JSON.stringify(itemsArray)
    },
    type : 'PUT',
    contentType : 'application/json'
  }).done(function(rsp) {
    console.log("done" + rsp);
  }).fail(function(err) {
    console.log("fail" + err);
  });
  </code></pre>
  
  <p>An on server is (in the doPut method):</p>
  
  <pre><code>    String jsonArray = request.getParameter("jsonArray"); // this is null
      // but if I read from request stream I get the data
      ServletInputStream inputStream = request.getInputStream();
  
      String string = convertStreamToString(inputStream); 
      System.out.println(string); // jsonArray=%5B%7B%22itemId%22%3A%228003%22%2C%22itemQuantity%22%3A%2210%22%7D%2C%7B%22itemId%22%3A%228004%22%2C%22itemQuantity%22%3A%222%22%7D%5D
  </code></pre>
  
  <p>The interesting part is that if change the method and send as POST I get get the parameter correctly.</p>
  
  <p>My guess is that Tomcat 6 needs some settings to get a parameter through PUT (url encoded.. etc).</p>
  
  <p>So, what should I do to send correctly JSON array through PUT method?</p>
  
  <p><strong>UPDATE</strong>:
  I am not sure if this is the right way to do it, but for me I find it easy to write myself a converter from message to parameter:</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.URLDecoder;
  import java.util.regex.Matcher;
  import java.util.regex.Pattern;
  import javax.servlet.ServletInputStream;
  
  public class RequestsService {
  
  public static String getDecodedMessage(ServletInputStream inputStream) throws IOException {
      BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
      String bodyMessageEncoded;
  
      bodyMessageEncoded = bf.readLine();
      String message = URLDecoder.decode(bodyMessageEncoded, "UTF-8");
      return message;
  
  }
  
  public static String getParameter(String url, String paramName) throws ArrayIndexOutOfBoundsException {
      Pattern pattern = Pattern.compile(paramName + "=[^&amp;]*"); // regex for "paramName='anything unless is &amp;'"
      Matcher matcher = pattern.matcher(url);
      matcher.find();
      String value = matcher.group().split("=")[1];
      return value;
  }
  
  public static String getParameterFromStream(ServletInputStream inputStream, String paramName) throws IOException {
      String decodedMessage = getDecodedMessage(inputStream);
      return getParameter(decodedMessage, paramName);
  }
  }
  </code></pre>