/
README
96 lines (63 loc) · 2.63 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
= SimpleHttp - a simplified wrapper around Net::Http
SimpleHttp aims to reduce the complexity of Net::Http while providing
the most commonly used (by me) http functionality.
INSTALLATION
* Using +gem+
gem install simplehttp
* Using +setup.rb+
ruby setup.rb config
ruby setup.rb install
* tarball and zip packages are available from
RubyForge[http://rubyforge.org/projects/simplehttp/]
FEATURES / USAGE
* Require the lib:
require 'simplehttp'
* No fuss one line GET and POST requests:
str = SimpleHttp.get "http://www.example.com"
str = SimpleHttp.get "www.example.com"
* Can use URI or String url interchangibly
str = SimpleHttp.get URI.parse "http://www.example.com/"
* Transparent Proxy Handling. Uses the 'http_proxy' environment
variable if set, also provides a +set_proxy+ method.
http = SimpleHttp.new "http://www.example.com"
http.set_proxy "http://proxy.example.com:8000"
http.post "query" => "example_query"
* POST sends ruby Hashes as 'application/x-www-form/urlencoded' per
default, but can send any data.
http = SimpleHttp.new "http://www.example.com/image_upload"
http.post imageData, "img/png"
* Automatically handles SSL
str = SimpleHttp.get "https://secure.example.com"
* Easy HTTP Basic Authentication
str = SimpleHttp.get URI.parse("http://usr:pwd@www.example.com")
#or
http = SimpleHttp.new "http://www.example.com"
http.basic_authentication "user", "password"
http.post "query" => "example_query"
* Access headers of the request or response
http = SimpleHttp.new "www.example.com"
http.request_headers["X-Custom-Header"]="useful header"
http.get
puts "server set cookie: #{http.response_headers['set-cookie']}"
* Automatically follows Http Redirects.
The +get+ and +post+ methods return a +String+ containing the
body of the request if the request was successful (HTTP 200). In case of
a redirect, the redirect is followed and the ultimate response is
returned. Per Default, up to three redirects are followed, this
behaviour can be modified by setting +follow_num_redirects+.
In case of any other type of response, an exception is raised.
The default behaviour can be modified by registering handlers
using the +register_response_handler+ method. E.g. if you'd like to
retrieve the +Date+ header instead of the body for successful
transactions:
http = SimpleHttp.new ...
http.register_response_handler(Net::HTTPSuccess) {|req,res,http|
res['date']
}
Or you'd like to print the +Location+ and then raise an exception in
case of a redirect:
http = SimpleHttp.new ...
http.register_response_handler(Net::HTTPRedirect) {|req,res,http|
puts res['location']
raise "REDIRECT not allowed!"
}