Permalink
Browse files

Adding all.

  • Loading branch information...
0 parents commit ed7558881674d285e7489b89ccb79d56e15b3457 August Lilleaas committed Jan 7, 2010
Showing with 156 additions and 0 deletions.
  1. +7 −0 README.textile
  2. +9 −0 basic_auth.rb
  3. +11 −0 headers.rb
  4. +14 −0 normal.rb
  5. +15 −0 post_form.rb
  6. +14 −0 response.rb
  7. +21 −0 rest.rb
  8. 0 shortcuts.rb
  9. +14 −0 ssl.rb
  10. +12 −0 ssl_with_pem.rb
  11. +9 −0 timeout.rb
  12. +25 −0 uri.rb
  13. +5 −0 without_uri.rb
@@ -0,0 +1,7 @@
+h1. Ruby Net::HTTP cheat sheet
+
+A bunch of common HTTP use cases implemented with Ruby's <code>Net::HTTP</code> library.
+
+h1. Fork
+
+If you want to add or change something, fork and send me a pull request.
@@ -0,0 +1,9 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://google.com/")
+
+http = Net::HTTP.new(uri.host, uri.port)
+request = Net::HTTP::Get.new(uri.request_uri)
+request.basic_auth("username", "password")
+response = http.request(request)
@@ -0,0 +1,11 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://google.com/")
+http = Net::HTTP.new(uri.host, uri.port)
+
+request = Net::HTTP::Get.new(uri.request_uri)
+request["User-Agent"] = "My Ruby Script"
+request["Accept"] = "*/*"
+
+response = http.request(request)
@@ -0,0 +1,14 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://google.com/")
+
+# Shortcut
+response = Net::HTTP.get_response(uri)
+
+# Will print response.body
+Net::HTTP.get_print(uri)
+
+# Full
+http = Net::HTTP.new(uri.host, uri.port)
+response = http.request(Net::HTTP::Get.new(uri.request_uri))
@@ -0,0 +1,15 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://example.com/search")
+
+# Shortcut
+response = Net::HTTP.post_form(uri, {"q" => "My query", "per_page" => "50"})
+
+# Full control
+http = Net::HTTP.new(uri.host, uri.port)
+
+request = Net::HTTP::Post.new(uri.request_uri)
+request.set_form_data({"q" => "My query", "per_page" => "50"})
+
+response = http.request(request)
@@ -0,0 +1,14 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://google.com/")
+
+http = Net::HTTP.new(uri.host, uri.port)
+request = Net::HTTP::Get.new(uri.request_uri)
+
+response = http.request(request)
+
+response.code # => 301
+response.body # => The body (HTML, XML, blob, whatever)
+# Headers are lowercased
+response["cache-control"] # => public, max-age=2592000
21 rest.rb
@@ -0,0 +1,21 @@
+# Basic REST.
+# Most REST APIs will set semantic values in response.body and response.code.
+require "net/http"
+
+http = Net::HTTP.new("api.restsite.com")
+
+request = Net::HTTP::Post.new("/users")
+request.set_form_data({"users[login]" => "quentin"})
+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::Delete.new("/users/1")
+response = http.request(request)
+
+request = Net::HTTP::Put.new("/users/1")
+request.set_form_data({"users[login]" => "changed"})
+response = http.request(request)
No changes.
14 ssl.rb
@@ -0,0 +1,14 @@
+require "net/https"
+require "uri"
+
+uri = URI.parse("https://secure.com")
+http =
+http.use_ssl = true
+http.verify_mode = OpenSSL::SSL::VERIFY_NONE
+
+request = Net::HTTP::Get.new(uri.request_uri)
+
+response = http.request(request)
+response.body
+response.status
+response["header-here"] # All headers are lowercase
@@ -0,0 +1,12 @@
+require "net/https"
+require "uri"
+
+uri = URI.parse("https://secure.com")
+pem = File.read("/path/to/my.pem")
+http = Net::HTTP.new(uri.host, uri.port)
+http.use_ssl = true
+http.cert = OpenSSL::X509::Certificate.new(pem)
+http.key = OpenSSL::PKey::RSA.new(pem)
+http.verify_mode = OpenSSL::SSL::VERIFY_PEER
+
+request = Net::HTTP::Get.new(uri.request_uri)
@@ -0,0 +1,9 @@
+require "net/http"
+require "uri"
+
+uri = URI.parse("http://google.com/")
+
+http = Net::HTTP.new(uri.host, uri.port)
+http.open_timeout = 3 # in seconds
+http.read_timeout = 3 # in seconds
+http.request(Net::HTTP::Get.new(uri.request_uri))
25 uri.rb
@@ -0,0 +1,25 @@
+require "uri"
+
+uri = URI.parse("http://mysite.com/some_api")
+uri = URI.parse("https://mysite.com/thing?foo=bar")
+
+# URI will also guess the correct port
+URI.parse("http://foo.com").port # => 80
+URI.parse("https://foo.com/").port # => 443
+
+# Full reference
+uri = URI.parse("http://foo.com/this/is/everything?query=params")
+# p (uri.methods - Object.methods).sort
+p uri.scheme # => "http"
+p uri.host # => "foo.com"
+p uri.port # => 80
+p uri.request_uri # => "/this/is/everything?query=params"
+p uri.path # => "/this/is/everything"
+p uri.query # => "query=params"
+
+# There are setters as well
+uri.port = 8080
+uri.host = "google.com"
+uri.scheme = "ftp"
+p uri.to_s
+# => "ftp://google.com:8080/this/is/everything?query=param"
@@ -0,0 +1,5 @@
+# You don't have to use URI.parse
+require "net/http"
+
+http = Net::HTTP.new("google.com", 80)
+response = http.request(Net::HTTP::Get.new("/foo/bar"))

2 comments on commit ed75588

On line 5, http = doesn't seem to make sense. Maybe http = Net::HTTP.new(uri.host, uri.port) instead?

Owner

augustl replied Jan 13, 2010

Thanks, fixed!

Please sign in to comment.