RestClient :: Request.execute传递hashset

时间:2013-01-30 04:36:14

标签: ruby-on-rails rest-client

我一直在使用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')的hashmap,就像我之前的调用中所能进行的那样。我该如何编写请求以便包含'post_params'。这是一个复杂的hashmap,所以我无法扩充或摆脱它。

非常感谢帮助。

2 个答案:

答案 0 :(得分:12)

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

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

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

答案 1 :(得分:3)

当我们尝试使用rest_client.post或任何方法(例如get,put 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)

这应该做

相关问题