Rails – speaking to rest as a mock webservice

27Feb09

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

Advertisement


No Responses Yet to “Rails – speaking to rest as a mock webservice”

  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.