Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a more user friendly alternative to Net::HTTP for interacting with REST APIs?

Tags:

rest

http

ruby

Net::HTTP can be rather cumbersome for the standard use case!

like image 916
mloughran Avatar asked Sep 19 '08 11:09

mloughran


People also ask

How REST API works internally in java?

Under REST architecture, the client and server can only interact in one way: The client sends a request to the server, then the server sends a response back to the client. Servers cannot make requests and clients cannot respond — all interactions are initiated by the client.

What is Restclient?

REST Client is a method or a tool to invoke a REST service API that is exposed for communication by any system or service provider. For example: if an API is exposed to get real time traffic information about a route from Google, the software/tool that invokes the Google traffic API is called the REST client.

How does REST Protocol work?

REST is a client-server architecture, where the server and the client act independently of one another as long as the interface stays the same when processing a request and a response. The server exposes the REST API, and the client makes use of it.


4 Answers

If you only have to deal with REST, the rest-client library is fantastic.

If the APIs you're using aren't completely RESTful - or even if they are - HTTParty is really worth checking out. It simplifies using REST APIs, as well as non-RESTful web APIs. Check out this code (copied from the above link):

require 'rubygems'
require 'httparty'

class Representative
  include HTTParty
  format :xml

  def self.find_by_zip(zip)
    get('http://whoismyrepresentative.com/whoismyrep.php', :query => {:zip => zip})
  end
end

puts Representative.find_by_zip(46544).inspect
# {"result"=>{"n"=>"1", "rep"=>{"name"=>"Joe Donnelly", "district"=>"2", "office"=>"1218 Longworth", "phone"=>"(202) 225-3915", "link"=>"http://donnelly.house.gov/", "state"=>"IN"}}}
like image 133
Clinton Dreisbach Avatar answered Sep 28 '22 00:09

Clinton Dreisbach


rest-open-uri is the one that is used heavily throughout the RESTful Web Services book.

gem install rest-open-uri

Example usage:

response = open('https://wherever/foo',
                :method => :put,
                :http_basic_authentication => ['my-user', 'my-passwd'],
                :body => 'payload')

puts response.read
like image 22
Aaron Hinni Avatar answered Sep 27 '22 23:09

Aaron Hinni


I'm a big fan of rest-client, which does just enough to be useful without getting in the way of your implementation. It handles exceptions intelligently, and supports logging and auth, out of the box.

like image 40
tomtaylor Avatar answered Sep 27 '22 23:09

tomtaylor


HyperactiveResource is in its infancy, but it's looking pretty good.

like image 42
Bill Turner Avatar answered Sep 27 '22 23:09

Bill Turner