1
我想在Scala中使用HttpURLConnection提交json發佈請求。我跟着跟着兩個教程和生產這樣的:使用HttpURLConnection提交json POST請求
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()
}
它與迴應:
Sent: '{"schedule":"R/2014-02-02T00:00:00Z/PT24H", "name":"Scala-Post-Test", "command":"which scalac", "epsilon":"PT15M", "owner":"[email protected]", "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
從
val in = new BufferedReader(new InputStreamReader(conn.getInputStream))
制止,但如果我重建爲curl
請求時,它工作正常:
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":"[email protected]", "async":false}' http://localhost:4040/scheduler/iso8601
requirement failed: Vertex already exists in graph Scala-Post-Test
(這是我期望的)
任何洞察到什麼是錯的?我正在嘗試嗅探數據包以確定不同之處。
(注:我以前在sys.process._放棄)
使用'PrintWriter'(而不是'DataOutputStream')。 –
^^暫時忽略它。雖然這裏通常不使用'DataOutputStream',但在這種情況下沒有任何區別。當你輸出'jsonHash'字符串時,它顯示你的JSON被單引號括起來;這可能是問題。 –
啊哈!你是正確的,單引號導致500. - 或許,nc -l現在看起來是一樣的,但400錯誤請求。搜索仍在繼續。 – Austin