Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestClient::Request.execute passing hashset

I have been using a RestClient request as such:

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

Which has been working fine. But I need to increase the timeout as it's not completing every now and then while the server is performing the upload.

I have researched and found that the only solution is to change the syntax to something like:

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

however, I don't seem to be able to pass the hashmap of parameters ('post_params') like i was able to in the previous call. how should I write the request so that 'post_params' is included. It's a complex hashmap, so i can't augment it or get rid of it.

Help is much appreciated.

like image 910
user2023973 Avatar asked Jan 30 '13 04:01

user2023973


2 Answers

The data you send is called a payload, so you need do specify it as payload:

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

Also, you may want to use a shorter timeout, otherwise there is a chance you get a Errno::EINVAL: Invalid argument.

like image 167
Pafjo Avatar answered Sep 27 '22 19:09

Pafjo


the data you send is in payload when we try to use rest_client.post or any method like get,put what rest_client do is

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

so like we want to execute

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

so in the execute command will take take {:file => file, :multipart => true } as payload and { :params =>{:foo => 'foo' } } as header so for passing all these you need

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

this should do

like image 34
manishbhadu Avatar answered Sep 27 '22 21:09

manishbhadu