title: cURL and HttpURLConnection - Post JSON Data
body: <p>How to post JSON data using HttpURLConnection? I am trying this:</p>
  
  <pre><code>HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
  httpcon.setDoOutput(true);
  httpcon.setRequestProperty("Content-Type", "application/json");
  httpcon.setRequestProperty("Accept", "application/json");
  httpcon.setRequestMethod("POST");
  httpcon.connect();
  
  StringReader reader = new StringReader("{'value': 7.5}");
  OutputStream os = httpcon.getOutputStream();
  
  char[] buffer = new char[4096];
  int bytes_read;    
  while((bytes_read = reader.read(buffer)) != -1) {
      os.write(buffer, 0, bytes_read);// I am getting compilation error here
  }
  os.close();
  </code></pre>
  
  <p>I am getting compilation error in line 14. </p>
  
  <p>The cURL request is:</p>
  
  <pre><code>curl -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d "{'value': 7.5}" \
  "a URL"
  </code></pre>
  
  <p>Is this the way to handle cURL request? Any information will be very helpful to me. </p>
  
  <p>Thanks.</p>