【问题标题】:How can I increase timeout for HTTParty post method in rails?如何增加 Rails 中 HTTParty 发布方法的超时时间?
【发布时间】:2018-05-21 07:58:49
【问题描述】:

我在 Rails 中有一个方法可以将发布请求发送到第三方 API。代码类似于以下内容:

data = HTTParty.post("url",
  :headers=> {'Content-Type' => 'application/json'},
  :body=> { update => true, first_name => "name" }
)

这样,在整整一分钟后,该过程终止并出现以下错误。

<Net::HTTPGatewayTimeOut 504 GATEWAY_TIMEOUT readbody=true>

【问题讨论】:

标签: ruby-on-rails ruby httparty


【解决方案1】:

通过以下方式设置默认值:

module HTTParty
  default_timeout your_preferred_timeout
end

或通过以下方式单独设置:

data = HTTParty.post("url",
  headers: {"Content-Type" => "application/json"},
  body: {update => true, first_name => "name"},
  timeout: your_preferred_timeout
)

【讨论】:

    【解决方案2】:

    你可以试试

     data = HTTParty.post("url",
      headers: {"Content-Type" => "application/json"},
      body: {update => true, first_name => "name"},
      open_timeout: 0.5,
      write_timeout:1,
      read_timeout:3
    )
    

    您也可以参考
    https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html#attribute-i-write_timeout

    【讨论】:

    • 适合httparty吗?它是网络::HTTP
    • 是的。 http派对
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    相关资源
    最近更新 更多