如何在rails中增加HTTParty post方法的超时?

时间:2018-05-21 07:58:50

标签: ruby-on-rails ruby httparty

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

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

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

<Net::HTTPGatewayTimeOut 504 GATEWAY_TIMEOUT readbody=true>

1 个答案:

答案 0 :(得分:7)

设置默认值:

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
)