title: Java read POST data when content-type is application/x-www-form-urlencoded
body: <p>I have an API that I'm testing. The API receives POST requests and reads it like this</p>
  
  <pre><code>      StringBuffer jb = new StringBuffer();
        String line = null;
        try {
          BufferedReader reader = request.getReader();
          while ((line = reader.readLine()) != null)
              jb.append(line);
  
          System.out.println("jb: "+jb);
          System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
  
        } catch (Exception e) { /*report an error*/ }
  </code></pre>
  
  <p>All works fine when I send a POST request in "application/json;charset=utf-8"</p>
  
  <pre><code>httpPost.setHeader("content-type", "application/json;charset=utf-8");
  </code></pre>
  
  <p>It prints this:</p>
  
  <pre><code>jb: {"client_domain":"=....); //proper Json data
  request.getHeader('content-type'): application/json;charset=utf-8
  </code></pre>
  
  <p>And I can read the data properly.</p>
  
  <p>However my problem is when I send the data the same way but I set the content-type "application/x-www-form-urlencoded;charset=utf-8"</p>
  
  <pre><code>httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
  </code></pre>
  
  <p>The test is the same just the content-type is different but then it seems that I don't receive any data anymore:</p>
  
  <pre><code>jb: 
  request.getHeader('content-type'): application/x-www-form-urlencoded;charset=utf-8
  </code></pre>
  
  <p>Any idea?</p>
  
  <p>/// Update</p>
  
  <p>Here is the Spring Controller </p>
  
  <pre><code>@RequestMapping(value = {"user/add"}, method = RequestMethod.POST, produces="application/json; charset=utf-8")
  @ResponseBody
  public ResponseEntity&lt;String&gt; getNewUserApi(HttpServletRequest request,
          HttpServletResponse response) throws Exception {
  
      Map&lt;String, Object&gt; jsonObj = new HashMap&lt;String, Object&gt;();
  
      StringBuffer jb = new StringBuffer();
        String line = null;
        try {
          BufferedReader reader = request.getReader();
          while ((line = reader.readLine()) != null)
              jb.append(line);
  
          System.out.println("jb: "+jb);
          System.out.println("request.getHeader('content-type'): "+request.getHeader("content-type"));
  
        } catch (Exception e) { /*report an error*/ }
      ///I create my JSon that will be sent back
      return JsonUtils.createJson(jsonObj);
  </code></pre>
  
  <p>//UPDATE 2
  Here is how I send the data</p>
  
  <pre><code>public static void main(String[] args) throws Exception {
  
      String url = "http://localhost:8080/child/apiv1/user/add";
      CloseableHttpClient client = HttpClientBuilder.create().build();
  
      HttpPost httpPost = new HttpPost(url);
      httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8");
  
      try {
          //we had to the parameters to the post request
          JSONObject json = new JSONObject();
  
          json.put("client_id", "fashfksajfhjsakfaskljhflakj");
          json.put("client_secret", "9435798243750923470925709348509275092");
          json.put("client_domain", "dummy.localhost.com");
  
          //create the user json object
          JSONObject userObj = new JSONObject();
          userObj.put("email", "johnsmith42@yopmail.com");
          userObj.put("name", "Anna Sax");
  
          JSONArray childrenArray = new JSONArray();
  
          JSONObject child1 = new JSONObject();
          child1.put("name", "Iphone 6");
          child1.put("age", "2");
          childrenArray.put(child1);
          userObj.put("children", childrenArray);
          json.put("user", childObj);
  
          StringEntity params = new StringEntity(json.toString());
          httpPost.setEntity(params);
  
          System.out.println("executing request: " + httpPost.getRequestLine());
          HttpResponse response;
          response = client.execute(httpPost);
  
     //[...]       
  
  } //End main
  </code></pre>
  
  <p>I know it doesn't really make sense to create Json and send it in "application/x-www-form-urlencoded" but it's just that one of our users can't fix his issue and it will only send "application/x-www-form-urlencoded".</p>