RestClient::Request.execute passing hashset
Asked Answered
G

2

8

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.

Griffey answered 30/1, 2013 at 4:36 Comment(0)
C
17

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.

Cadastre answered 30/1, 2013 at 6:32 Comment(0)
T
4

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

Titanothere answered 28/6, 2017 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.