使用 HTTParty 发布大量数据

发布于 2025-01-03 07:15:49 字数 489 浏览 2 评论 0原文

我使用 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)

问题是,当 POST 实际执行时,我收到一条 Connection Reset by Peer (Errno::ECONNRESET) 消息,我'我很确定这是由于我的有效负载太大造成的(因为 logs_to_push 是一个包含约 200 条日志行的数组)。我如何重构上述内容才能成功推送这些数据?

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)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘦怹 2025-01-10 07:15:49

因此,事实证明,对于大量内容,您应该将有效负载放入 :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)

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)
〆凄凉。 2025-01-10 07:15:49

尝试这个帖子请求

require 'httparty'
require 'json'

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

谢谢

Try This for Post Req

require 'httparty'
require 'json'

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

Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文