Rails – speaking to rest as a mock webservice
Finally understood the distinctions between webservices and rest. Now I don’t mean the theory aspects Roy Fieldings paper adequately explains the concepts and frankly helps a great deal to try to introduce technology which is along the lines of ‘less is more’.
What I mean is being able to create a service mock up a test script and talk to the server using XML.
Here is an example of creating a new Person:
require 'net/http'
s = <<-XML
Sarah
Jhummun
XML
Net::HTTP.start('localhost', 3000) do |http|
response = http.post('/people.xml', "#{s}", 'Content-Type' => 'text/xml')
#Do something with the response.
puts "Code: #{response.code}"
puts "Message: #{response.message}"
puts "Body:\n #{response.body}"
end
and here is an example of retrieving a list of people:
require 'net/http'
Net::HTTP.start('localhost', 3000) do |http|
response = http.get('/people/', 'Accept' => 'text/xml')
#Do something with the response.
puts "Code: #{response.code}"
puts "Message: #{response.message}"
puts "Body:\n #{response.body}"
end
Filed under: 1 | Leave a Comment
Tags: rails, rest
No Responses Yet to “Rails – speaking to rest as a mock webservice”