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