Module: RestClient
- Defined in:
- lib/restclient.rb,
lib/restclient/request.rb,
lib/restclient/resource.rb,
lib/restclient/response.rb,
lib/restclient/exceptions.rb
Overview
This module’s static methods are the entry point for using the REST client.
# GET
xml = RestClient.get 'http://example.com/resource'
jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
# authentication and SSL
RestClient.get 'https://user:[email protected]/private/resource'
# POST or PUT with a hash sends parameters as a urlencoded form body
RestClient.post 'http://example.com/resource', :param1 => 'one'
# nest hash parameters
RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
# POST and PUT with raw payloads
RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
RestClient.post 'http://example.com/resource.xml', xml_doc
RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
# DELETE
RestClient.delete 'http://example.com/resource'
# retreive the response http code and headers
res = RestClient.get 'http://example.com/some.jpg'
res.code # => 200
res.headers[:content_type] # => 'image/jpg'
# HEAD
RestClient.head('http://example.com').headers
To use with a proxy, just set RestClient.proxy to the proper http proxy:
RestClient.proxy = "http://proxy.example.com/"
Or inherit the proxy from the environment:
RestClient.proxy = ENV['http_proxy']
For live tests of RestClient, try using rest-test.heroku.com, which echoes back information about the rest call:
>> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
=> "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
Defined Under Namespace
Classes: Exception, ExceptionWithResponse, NotModified, Redirect, Request, RequestFailed, RequestTimeout, Resource, ResourceNotFound, Response, ServerBrokeConnection, Unauthorized
Class Attribute Summary collapse
-
.proxy ⇒ Object
Returns the value of attribute proxy.
Class Method Summary collapse
- .delete(url, headers = {}) ⇒ Object
- .get(url, headers = {}) ⇒ Object
- .head(url, headers = {}) ⇒ Object
-
.log ⇒ Object
:nodoc:.
-
.log=(log) ⇒ Object
Print log of RestClient calls.
- .post(url, payload, headers = {}) ⇒ Object
- .put(url, payload, headers = {}) ⇒ Object
Class Attribute Details
.proxy ⇒ Object
Returns the value of attribute proxy.
77 78 79 |
# File 'lib/restclient.rb', line 77 def proxy @proxy end |
Class Method Details
.delete(url, headers = {}) ⇒ Object
68 69 70 |
# File 'lib/restclient.rb', line 68 def self.delete(url, headers={}) Request.execute(:method => :delete, :url => url, :headers => headers) end |
.get(url, headers = {}) ⇒ Object
56 57 58 |
# File 'lib/restclient.rb', line 56 def self.get(url, headers={}) Request.execute(:method => :get, :url => url, :headers => headers) end |
.head(url, headers = {}) ⇒ Object
72 73 74 |
# File 'lib/restclient.rb', line 72 def self.head(url, headers={}) Request.execute(:method => :head, :url => url, :headers => headers) end |
.log ⇒ Object
:nodoc:
86 87 88 89 90 |
# File 'lib/restclient.rb', line 86 def self.log # :nodoc: return ENV['RESTCLIENT_LOG'] if ENV['RESTCLIENT_LOG'] return @@log if defined? @@log nil end |
.log=(log) ⇒ Object
Print log of RestClient calls. Value can be stdout, stderr, or a filename. You can also configure logging by the environment variable RESTCLIENT_LOG.
82 83 84 |
# File 'lib/restclient.rb', line 82 def self.log=(log) @@log = log end |