title: Apache HttpClient JSON Post
body: <p>I try to send directly a JSON string with HttpClient 4.4 in an application (SWT/JFace) :</p>
  
  <pre><code>    public String postJSON(String urlToRead,Object o) throws ClientProtocolException, IOException {
      String result="";
      CloseableHttpClient httpClient = HttpClients.createDefault();
      try {
          HttpPost postRequest = new HttpPost(urlToRead);
          postRequest.setHeader("content-type", "application/x-www-form-urlencoded");
          //postRequest.setHeader("Content-type", "application/json");
  
          //{"mail":"admin@localhost", "password":"xyz"}
          String jsonString=gson.toJson(o);
          StringEntity params =new StringEntity(jsonString);
          params.setContentType("application/json");
          params.setContentEncoding("UTF-8");
          postRequest.setEntity(params);
          ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
          result = httpClient.execute(postRequest, responseHandler);
      }finally {
          httpClient.close();;
      }
      return result;
  }
  </code></pre>
  
  <p>I try to get the response from the server (Apache/PHP) with <code>$POST</code></p>
  
  <p>the correct content of <code>$POST</code> should be :</p>
  
  <pre><code>array("mail"=&gt;"admin@localhost","password"=&gt;"xyz")
  </code></pre>
  
  <p>When I use <code>content-type : application/x-www-form-urlencoded</code></p>
  
  <p><code>$POST</code> content is :</p>
  
  <pre><code>array( "{"mail":"admin@localhost","password":"xyz"}"=&gt; )
  </code></pre>
  
  <p>When I use <code>content-type : application/json</code></p>
  
  <p><code>$POST</code> is empty : array()</p>
  
  <p>Is there a way to post the JSON string with HttpClient or should I use an <code>ArrayList&lt;NameValuePair&gt;</code> and add each member of my object in the entity ?</p>