Connecting to web services using Rails (HTTP requests)?

17,992

Solution 1

Yes, since you're using RESTful routes, they are your basic API. You're also returning structured JSON data easily consumable by an application.

There are other ways to implement a web services API (e.g. SOAP), but this is a good and proper way.

Since it's a web services API, connecting to the correct URL and parsing the response is the way to go on the client side. Though if you need to access many different resources it might be a good idea to create a flexible way of building the request URL.

Solution 2

If you don't need low-level tweak-ability offered by Net::HTTP, instead take a look at using Open-URI, which comes with Ruby. It makes it easy to request a page and receive the body back. Open-URI doesn't have all the bells and whistles but for a lot of what I do it's plenty good.

A simple use looks like:

require 'open-uri'
body = open('http://www.example.com').read

The docs have many other examples.

These are other HTTP clients I like:

They are more tweakable and can handle multiple connections at once if that's what you need. For instance, Typhoeus has a suite of simplified calls, similar to Open-URI's. From the docs:

response = Typhoeus::Request.get("http://www.pauldix.net")
response = Typhoeus::Request.head("http://www.pauldix.net")
response = Typhoeus::Request.put("http://localhost:3000/posts/1", :body => "whoo, a body")
response = Typhoeus::Request.post("http://localhost:3000/posts", :params => {:title => "test post", :content => "this is my test"})
response = Typhoeus::Request.delete("http://localhost:3000/posts/1")

HTTPClient has similar shortened methods too.

Solution 3

I'd recommend using Rails' ActiveResource for most cases. Failing that, httparty.

Solution 4

I would use ActiveResource if you just need a simple way to pull in rest-based resources. It's already included in rails and pretty trivial to set up. You just specify a base url and resource name in your ActiveResource subclass and then you can CRUD rest-based resources with an interface similar to that of ActiveRecord.

Solution 5

rest-client is the most popular gem for easily connecting to RESTful web services

Share:
17,992
user502052
Author by

user502052

Updated on June 06, 2022

Comments

  • user502052
    user502052 almost 2 years

    I am using Ruby on Rails 3 and I am trying to implement APIs to retrieve account information from a web service. That is, I would like to connect to a web service that has the Account class and get information from the show action routed at the URI http://<site_name>/accounts/1.

    At this time, in the web service accounts_controller.rb file I have:

    class AccountsController < ApplicationController
      def show
        @account = Account.find(params[:id])
    
        respond_to do |format|
          format.html
          format.js
          format.json { render :json => @account.to_json }
        end
      end
    end
    

    Now I need some advice for connecting to the web service. In the client application, I should have a HTTP GET request, but here is my question: What is "the best" approach to connect to a web service making HTTP requests?

    This code in the client application works:

    url = URI.parse('http://<site_name>/accounts/1.json')
    req = Net::HTTP::Get.new(url.path)
    res = Net::HTTP.start(url.host, url.port) {|http|
      http.request(req)
    }
    
    @output = JSON(res.body)["account"]
    

    but, is the above code "the way" to implement APIs?

    Is it advisable to use third-party plugins and gems?