title: Using HttpURLConnection to submit a json POST request
body: <p>I am trying to submit a json post request using HttpURLConnection in Scala. I followed along to two tutorials and produced this:</p>
  
  <pre><code>def sendPost(url: String, jsonHash: String) {
    val conn: HttpURLConnection = new URL(url).openConnection().asInstanceOf[HttpURLConnection]
    conn.setRequestMethod("POST")
    conn.setRequestProperty("Content-Type", "application/json")
    conn.setRequestProperty("Accept", "application/json")
    conn.setDoOutput(true)
    conn.connect()
  
    val wr = new DataOutputStream(conn.getOutputStream)
    wr.writeBytes(jsonHash)
    wr.flush()
    wr.close()
  
    val responseCode = conn.getResponseCode
  
    println("Sent: " + jsonHash + " to " + url + " received " + responseCode)
  
    val in = new BufferedReader(new InputStreamReader(conn.getInputStream))
    var response: String = ""
    while(response != null) {
      response = in.readLine()
      println(response)
    }
    in.close()
  }
  </code></pre>
  
  <p>It responds with:</p>
  
  <pre><code>Sent: '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' to http://localhost:4040/scheduler/iso8601 received 500 
  
  java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:4040/scheduler/iso8601
  </code></pre>
  
  <p>stemming from </p>
  
  <pre><code>val in = new BufferedReader(new InputStreamReader(conn.getInputStream))
  </code></pre>
  
  <p>but if I rebuild it as a <code>curl</code> request, it works fine:</p>
  
  <pre><code>curl -X POST -H 'Content-Type: application/json' -d '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"myemail@thecompany.com", "async":false}' http://localhost:4040/scheduler/iso8601
  
  requirement failed: Vertex already exists in graph Scala-Post-Test
  </code></pre>
  
  <p>(which is what I expect)</p>
  
  <p>Any insight to what is wrong? I'm trying to sniff the packets now to determine what is different.</p>
  
  <p>(Note: I had previously given up on <a href="https://stackoverflow.com/questions/21247653/using-scala-sys-process-with-curl-single-quotes-and-white-space">sys.process._</a>)</p>