2012-02-07 66 views
2

我使用HTTParty使用下面的代碼發佈信息到服務器:POST操作大量的數據與HTTParty

this_component = {"name" => "something", "ip" => "localhost", "logs" => logs_to_push} 
payload = {"payload" => JSON.dump(this_component)} 
response = JSONClient.post("http://localhost:8080/log", :body => '', :query => payload) 

的問題是,我得到了Connection reset by peer (Errno::ECONNRESET)消息POST實際執行的時候,這是我很可能是由於我的有效負載太大造成的(因爲logs_to_push是一個包含〜200個日誌行的數組)。我將如何重構上述內容以便成功推送這些數據?

回答

3

所以事實證明,對於大量的東西,你應該把有效載荷在:body而不是:query。對於遇到這個問題未來的人,正確的代碼(工作過上面的例子)將是:

this_component = {"name" => "something", "ip" => "localhost", "logs" => logs_to_push} 
payload = {"body" => {"payload" => JSON.dump(this_component)}} 
response = JSONClient.post("http://localhost:8080/log", payload) 
+1

這其實是不相關到HTTParty,但發送請求的服務器。最大長度取決於服務器軟件。在這篇文章中看到一些值http://stackoverflow.com/questions/812925/what-is-the-maximum-possible-length-of-a-query-string – 2012-10-05 12:41:41

1

嘗試一下本作後詢價

require 'httparty' 
require 'json' 

load = {:name => "xyz",:logs => "xyz"} 
payload = load.to_json 
url="http://xyz.com/abc" 
response = HttParty.post(url,{:body => payload}) 

感謝