8

我一直在使用 RestClient 请求:

response = RestClient.post server_url, post_params, accept: :json

哪个工作正常。但是我需要增加超时,因为它在服务器执行上传时不时完成。

我研究并发现唯一的解决方案是将语法更改为:

response = RestClient::Request.execute(:method => :post, :url => server_url, post_params, :timeout => 9000000000)

但是,我似乎无法'post_params'像在之前的调用中那样传递参数 ( ) 的哈希图。我应该如何编写请求以便'post_params'包含在内。这是一个复杂的哈希图,所以我无法增加或摆脱它。

非常感谢您的帮助。

4

2 回答 2

17

您发送的数据称为有效负载,因此您需要将其指定为有效负载:

response = RestClient::Request.execute(:method => :post, :url => server_url, :payload => post_params, :timeout => 9000000, :headers => {:accept => :json})

此外,您可能希望使用更短的超时,否则您可能会得到 Errno::EINVAL: Invalid 参数。

于 2013-01-30T06:32:31.313 回答
4

当我们尝试使用 rest_client.post 或 get 之类的任何方法时,您发送的数据在有效负载中,把rest_client做的是

def self.post(url, payload, headers={}, &block)
    Request.execute(:method => :post, :url => url, :payload => payload, 
                    :headers => headers, &block)
end

所以就像我们想要执行

 response = RestClient.post api_url,
             {:file => file, :multipart => true },
             { :params =>{:foo => 'foo'} #query params

所以在执行命令中将采取{:file => file, :multipart => true }作为有效载荷和{ :params =>{:foo => 'foo' } }作为标题,以便传递所有这些你需要

response= RestClient::Request.execute(:method => :post, 
                                     :url => api_url,
                                     :payload => {:file => file, :multipart => true },
                                     :headers => { :params =>{:foo => 'foo'}},
                                      :timeout => 90000000)

这应该做

于 2017-06-28T10:51:09.140 回答