Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST Client Example in Ruby

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.

like image 967
rubythemystery Avatar asked Dec 09 '11 22:12

rubythemystery


People also ask

What is REST client in Ruby?

REST Client -- simple DSL for accessing HTTP and REST resources. A simple HTTP and REST client for Ruby, inspired by the Sinatra's microframework style of specifying actions: get, put, post, delete.

What is client type REST?

It separates clients from servers. Separating the UI from the data storage improves the portability of the UI across platforms. It improves scalability through simplification of the server components. This separation enables the components to evolve independently.

What is the difference between RESTClient and Httpclient?

HTTP client is a client that is able to send a request to and get a response from the server in HTTP format. REST client is a client that is designed to use a service from a server and this service is RESTful.


2 Answers

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

like image 149
jmontross Avatar answered Sep 28 '22 17:09

jmontross


The Readme file at the git site for the rest-client gem has a whole bunch of examples of how to do requests, include parameters, etc.

I'd start with that.

If there are specific things that are not working, then it generally helps to post the code you've tried that you think SHOULD be working, and then it's usually easier for people to tell where you are going wrong.

like image 36
Chuck van der Linden Avatar answered Sep 28 '22 18:09

Chuck van der Linden