title: HttpClient POST request returns status 200 without any body (however it should be). Length of the content is -1
body: <p>I use Apache HttpClient for POST requests to web service. 
  I'm getting </p>
  
  <blockquote>
    <p>httpResult=200</p>
  </blockquote>
  
  <p>however there is no body. I know that some body should be there as <strong>when I
  use another POST call method then I'm getting the body in JSON format.</strong> </p>
  
  <p>At this method the length of the response body = -1.</p>
  
  <blockquote>
    <p><strong>response.getEntity().getContentLength()</strong> = -1;</p>
    
    <p>The result of <strong>EntityUtils.toString(response.getEntity())</strong> is empty string.</p>
  </blockquote>
  
  <p>The code is:</p>
  
  <pre><code>    CloseableHttpClient client = HttpClients.createDefault();
      HttpPost httpPost = new HttpPost(url);
  
      JSONObject attributes = new JSONObject();
      JSONObject main = new JSONObject();
  
      attributes.put("201", "Frank");
      main.put("attributes", attributes);
      main.put("primary", "2");
  
      String json = main.toString();
  
      StringEntity entity = new StringEntity(json);
      httpPost.setEntity(entity);
      httpPost.setHeader("Accept", "application/json");
      httpPost.setHeader("Content-type", "application/json");
  
      CloseableHttpResponse response = client.execute(httpPost);
      httpResult = response.getStatusLine().getStatusCode();
  
      client.close();
  
      if (httpResult == HttpURLConnection.HTTP_OK) {
  
          HttpEntity ent = response.getEntity();
  
          Long length = ent.getContentLength();
  
          System.out.println("Length: " + length);// length = -1
  
       }
  </code></pre>
  
  <p>Could anyone give my some hint how to solve the issue?</p>
  
  <p><strong>In addition</strong> I would like to add the code that gives me a correct response body. At this case I use HttpURLConnection.</p>
  
  <pre><code>        HttpURLConnection urlConnect = (HttpURLConnection) url.openConnection();
          urlConnect.setConnectTimeout(10000);
  
          urlConnect.setRequestProperty("Accept", "application/json");
          urlConnect.setRequestProperty("Content-Type", "application/json");
          urlConnect.setRequestMethod("POST");
  
          JSONObject attributes = new JSONObject();
          JSONObject main = new JSONObject();
  
          attributes.put("201", "Frank");
          main.put("primary", "2");
          main.put("attributes", attributes);
  
          urlConnect.setDoOutput(true);
  
          OutputStreamWriter wr = new OutputStreamWriter(urlConnect.getOutputStream());
          wr.write(main.toString());
          wr.flush();
  
          httpResult = urlConnect.getResponseCode();
  
          System.out.println("Http Result: " + httpResult);
  
          if (httpResult == HttpURLConnection.HTTP_OK) {
  
              InputStream response = urlConnect.getInputStream(); // correct not empty response body 
  
              ...
          } 
  </code></pre>