REST methods form request in Ruby On Rails Client

In order to access RESTful web services from Ruby Client script using form request. The method net/http tries to connect over HTTP even of the uri is HTTPS. For HTTPS, You need to explicitly tell net/http that a secure connection should be used. REST client code as shown below:

# Basic REST.
# Most REST APIs will set semantic values in response.body and response.code.

require “net/http”

http = Net::HTTP.new(“api.domainname.com”)

request = Net::HTTP::Post.new(“/users”)
request.set_form_data({“users[login]” => “MyUser”})
response = http.request(request)
# Use nokogiri, hpricot, etc to parse response.body.

request = Net::HTTP::Get.new(“/users/1”)
response = http.request(request)
# As with POST, the data is in response.body.

request = Net::HTTP::Put.new(“/users/1”)
request.set_form_data({“users[login]” => “changed”})
response = http.request(request)

request = Net::HTTP::Delete.new(“/users/1”)
response = http.request(request)

Permanent link to this article: https://blog.openshell.in/2011/03/rest-methods-form-request-in-ruby-on-rails-client/

Leave a Reply

Your email address will not be published.