Interacting with a REST interface from another application


I’m trying to work out how to hook a couple of my applications together
with RESTful APIs. I have done it previously with ActionWebService
(both apps are Rails), which kindly provides structures for both
creating APIs and calling them. What I’m not clear about is how to set
up one application to GET, POST, PUT, or DELETE from another. Perhaps
ActiveResource will solve this nicely when it’s ready for prime time,
but in the meantime, is there are straightforward way to do this?

I guess the heart of my question would be “How do I call a particular
URL from within a model or controller, and then process the XML (or
whatever it is) that comes back?” Once I can do this, then I still have
to work out a nice way to generate the URLs to call, but that I know I
can figure out how to do.


Generating XML is very easy in RESTful Rails, consuming it is left to
the developer. Any RESTful URL link that ends with xml will fetch the
XML document. In your controller you can do something like:

require “net/http”

my_site =“”, 8080)
response, data = my_site.get(“products.xml”, nil)
rescue => err
puts “Error: #{err}”

puts “Received #{data.split.size} lines, #{data.size} bytes”

Parse your XML document here…

Remember that security is not handled here. One way to implement the
other http functions is to look at the Rails 1.2 RC1 source code. You
will learn a lot from it. HTH.