REST Client Example in Ruby

rubythemystery picture rubythemystery · Dec 9, 2011 · Viewed 56.9k times · Source

Can anyone explain me with an example, by using REST Client to do GET/POST/PUT operations in a Rest web service?

In POST/PUT, using REST Client, need to pass the whole xml body to do POST/PUT operations.

For example, Using REST Client

I need to get the content of a service using,

      RESTClient.get(url)

POST an xml to an url:

      RESTClient.post(url,entirexml)

PUT an xml to an URL:

      RESTClient.put(url,entirexml)

DELETE using REST CLIENT.

Can anyone help me with examples for all the REST Client HTTP METHODS with example?

I need to send the whole XML along with namespace to a rest service using PUT/POST operations of REST Client.

If anyone have examples on this, kindly post then please.

Answer

jmontross picture jmontross · Nov 16, 2012
require 'rest-client'

RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}

RestClient.get 'http://example.com/resource'

xml = '<xml><foo>bar</foo><bar>foo</bar></xml>'

RestClient.post 'http://example.com/resource', xml , {:content_type => :xml}

RestClient.put 'http://example.com/resource', xml , {:content_type => :xml}

RestClient.delete 'http://example.com/resource'

See more examples and documentation at https://github.com/rest-client/rest-client