Rails – speaking to rest as a mock webservice

2009
02.27

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

Related posts:

  1. Secure Copy How to Heres a rough and ready introduction to using the power...

Related posts brought to you by Yet Another Related Posts Plugin.

Tags: ,

One Response to “Rails – speaking to rest as a mock webservice”

  1. Great post! I’ll subscribe right now wth my feedreader software!

Your Reply