使用HTTParty发布大量数据 [英] POSTing large amounts of data with HTTParty
问题描述
我正在使用HTTParty使用以下代码将信息发布到服务器:
I'm using HTTParty to post information to a server using the following code:
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)
问题是当POST实际执行时,我会收到一条Connection reset by peer (Errno::ECONNRESET)
消息,我可以肯定这是由于我的有效载荷过大造成的(因为logs_to_push
是其中包含约200条日志行的数组) .我将如何重构以上内容,以便我可以成功推送此数据?
The problem is that I get a Connection reset by peer (Errno::ECONNRESET)
message when the POST actually executes, which I'm pretty sure is caused by my payload being too large (as logs_to_push
is an array with ~200 log lines in it). How would I refactor the above so that I could push this data successfully?
推荐答案
因此,对于大量内容,您应该将有效负载放在:body
中而不是:query
中.对于将来遇到此问题的人们,正确的代码(适用于上面的示例)将是:
So it turns out that for large amount of stuff, you should put the payload in :body
and not :query
. For future people that run into this problem, the correct code (working off the above example) would be:
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)
这篇关于使用HTTParty发布大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!