Search code examples
ruby-on-railsrubyrest-client

What is the difference between RestClient::Request.execute and RestClient.post?


I made a post request with RestClient::Request.execute, which works, but sometimes it ended with a 422(Unprocessable Entity).

Afterwards I tried out RestClient.post which didn´t gave me the 422 and worked all the time like a charm.

What is the difference between the two Calls?

I know that with RestClient::Request there are more possibilities for using parameters than with RestClient.post. I do not understand why i get a 422 with one method and not with the other.

Here I used json:

response = RestClient::Request.execute(
        :method => :post,
        :url =>  'http://localhost:3000',
        :timeout => 30,
        :open_timeout => 2,
        :payload => payload.to_json,
        :headers => {
            :content_type => :json,
            :accept => :json
        }
    )

vs.

response = RestClient.post('http://localhost:3000',
          :param1 => 'abc',
          :param2 => "def")

Solution

  • They are the same - RestClient.post is a syntax sugar for execute, see restclient's sources, restclient.rb:

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

    The 422 is caused by something else