rest-client 2.0.0.rc2-x86-mswin32 → 2.0.0.rc3-x86-mswin32
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop-disables.yml +17 -8
- data/.travis.yml +32 -2
- data/AUTHORS +6 -0
- data/README.md +578 -0
- data/Rakefile +1 -1
- data/history.md +45 -13
- data/lib/restclient.rb +4 -2
- data/lib/restclient/abstract_response.rb +51 -25
- data/lib/restclient/exceptions.rb +45 -6
- data/lib/restclient/params_array.rb +72 -0
- data/lib/restclient/payload.rb +40 -69
- data/lib/restclient/raw_response.rb +1 -2
- data/lib/restclient/request.rb +372 -199
- data/lib/restclient/response.rb +11 -8
- data/lib/restclient/utils.rb +144 -2
- data/lib/restclient/version.rb +1 -1
- data/rest-client.gemspec +5 -5
- data/spec/helpers.rb +8 -0
- data/spec/integration/httpbin_spec.rb +7 -7
- data/spec/integration/integration_spec.rb +34 -24
- data/spec/integration/request_spec.rb +1 -1
- data/spec/spec_helper.rb +8 -1
- data/spec/unit/abstract_response_spec.rb +76 -33
- data/spec/unit/exceptions_spec.rb +27 -21
- data/spec/unit/params_array_spec.rb +36 -0
- data/spec/unit/payload_spec.rb +71 -53
- data/spec/unit/raw_response_spec.rb +3 -3
- data/spec/unit/request2_spec.rb +29 -7
- data/spec/unit/request_spec.rb +552 -415
- data/spec/unit/resource_spec.rb +25 -25
- data/spec/unit/response_spec.rb +86 -64
- data/spec/unit/restclient_spec.rb +13 -13
- data/spec/unit/utils_spec.rb +117 -41
- data/spec/unit/windows/root_certs_spec.rb +2 -2
- metadata +15 -12
- data/README.rdoc +0 -410
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9619b56074f57c7b3a3781da1f0e7132f151d4b1
|
4
|
+
data.tar.gz: c5f4ece186655aa494474fb30351743520dc619d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be4a0eb8bf0210f241e3f835b1f8776c16b09ac8c215120ce8de9cc83d02acc0258aa826b939e918a4965781654d6aa22ead967a8fe303b9b3f5d618f6f1b11c
|
7
|
+
data.tar.gz: cee75b4f75326e37aeb59cae88d436dcfecc67b584102ea2ddc9bbdd63115cc80a5200cac5df43db248da25bf9ab1d100610398302db142febf0b2f56f24615c
|
data/.gitignore
CHANGED
data/.rubocop-disables.yml
CHANGED
@@ -21,6 +21,14 @@ Lint/Eval:
|
|
21
21
|
Exclude:
|
22
22
|
- rest-client.windows.gemspec
|
23
23
|
|
24
|
+
Lint/HandleExceptions:
|
25
|
+
Exclude:
|
26
|
+
- lib/restclient/utils.rb
|
27
|
+
|
28
|
+
Lint/UselessAccessModifier:
|
29
|
+
Exclude:
|
30
|
+
- lib/restclient/windows/root_certs.rb
|
31
|
+
|
24
32
|
# Offense count: 4
|
25
33
|
# Cop supports --auto-correct.
|
26
34
|
Style/Alias:
|
@@ -35,7 +43,7 @@ Style/AndOr:
|
|
35
43
|
# TODO
|
36
44
|
# Offense count: 3
|
37
45
|
# Cop supports --auto-correct.
|
38
|
-
Style/
|
46
|
+
Style/BlockDelimiters:
|
39
47
|
Enabled: false
|
40
48
|
|
41
49
|
# Offense count: 48
|
@@ -90,6 +98,9 @@ Style/CollectionMethods:
|
|
90
98
|
Style/ColonMethodCall:
|
91
99
|
Enabled: false
|
92
100
|
|
101
|
+
Style/ConditionalAssignment:
|
102
|
+
EnforcedStyle: assign_inside_condition
|
103
|
+
|
93
104
|
# Offense count: 2
|
94
105
|
Style/ConstantName:
|
95
106
|
Enabled: false
|
@@ -161,12 +172,11 @@ Style/FileName:
|
|
161
172
|
Style/FormatString:
|
162
173
|
Enabled: false
|
163
174
|
|
164
|
-
# TODO:
|
165
|
-
# Offense count: 514
|
175
|
+
# TODO: enable
|
166
176
|
# Cop supports --auto-correct.
|
167
177
|
# Configuration parameters: SupportedStyles.
|
168
178
|
Style/HashSyntax:
|
169
|
-
|
179
|
+
Enabled: false
|
170
180
|
|
171
181
|
# NOTABUG
|
172
182
|
# Offense count: 8
|
@@ -337,10 +347,9 @@ Style/SpaceInsideParens:
|
|
337
347
|
Style/StringLiterals:
|
338
348
|
Enabled: false
|
339
349
|
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
Style/TrailingComma:
|
350
|
+
Style/TrailingCommaInLiteral:
|
351
|
+
EnforcedStyleForMultiline: comma
|
352
|
+
Style/TrailingCommaInArguments:
|
344
353
|
Enabled: false
|
345
354
|
|
346
355
|
# TODO: configure
|
data/.travis.yml
CHANGED
@@ -1,13 +1,43 @@
|
|
1
|
+
# Available ruby versions: http://rubies.travis-ci.org/
|
2
|
+
|
1
3
|
language: ruby
|
4
|
+
os:
|
5
|
+
- linux
|
6
|
+
- osx
|
2
7
|
rvm:
|
3
|
-
- "1.9.3"
|
4
8
|
- "2.0.0"
|
5
9
|
- "2.1" # latest 2.1.x
|
6
10
|
- "2.2" # latest 2.2.x
|
7
|
-
- "
|
11
|
+
- "2.3.1" # TODO: switch to "2.3" once travis fixes it
|
12
|
+
- "ruby-head"
|
13
|
+
- "jruby-9.0.5.0"
|
8
14
|
- "jruby-head"
|
9
15
|
script:
|
10
16
|
bundle exec rake test
|
11
17
|
branches:
|
12
18
|
except:
|
13
19
|
- "readme-edits"
|
20
|
+
|
21
|
+
# Travis OS X support is pretty janky. These are some hacks to include tests
|
22
|
+
# only on versions that actually work.
|
23
|
+
# (last tested: 2016-04)
|
24
|
+
matrix:
|
25
|
+
exclude:
|
26
|
+
- os: osx
|
27
|
+
rvm: '2.2'
|
28
|
+
- os: osx
|
29
|
+
rvm: '2.3.1' # No 2.3.x at all
|
30
|
+
include:
|
31
|
+
- os: osx
|
32
|
+
rvm: '2.2.2' # Travis OS X doesn't have 2.2 aliases
|
33
|
+
allow_failures:
|
34
|
+
- rvm: 'ruby-head'
|
35
|
+
- os: osx
|
36
|
+
rvm: 'jruby-9.0.5.0'
|
37
|
+
- os: osx
|
38
|
+
rvm: 'jruby-head'
|
39
|
+
- os: linux
|
40
|
+
rvm: 'jruby-head'
|
41
|
+
fast_finish: true
|
42
|
+
|
43
|
+
sudo: false
|
data/AUTHORS
CHANGED
@@ -3,6 +3,7 @@ the following kind souls:
|
|
3
3
|
|
4
4
|
Adam Jacob
|
5
5
|
Adam Wiggins
|
6
|
+
Adrian Rangel
|
6
7
|
Alex Tomlins
|
7
8
|
Aman Gupta
|
8
9
|
Andy Brody
|
@@ -19,6 +20,7 @@ Crawford
|
|
19
20
|
Cyril Rohr
|
20
21
|
Dan Mayer
|
21
22
|
Dario Hamidi
|
23
|
+
Darren Coxall
|
22
24
|
David Backeus
|
23
25
|
David Perkowski
|
24
26
|
Dmitri Dolguikh
|
@@ -50,6 +52,7 @@ Jordi Massaguer Pla
|
|
50
52
|
Joshua J. Campoverde
|
51
53
|
Juan Alvarez
|
52
54
|
Julien Kirch
|
55
|
+
Jun Aruga
|
53
56
|
Justin Coyne
|
54
57
|
Justin Lambert
|
55
58
|
Keith Rarick
|
@@ -63,6 +66,7 @@ Lee Jarvis
|
|
63
66
|
Lennon Day-Reynolds
|
64
67
|
Lin Jen-Shin
|
65
68
|
Marc-André Cournoyer
|
69
|
+
Marius Butuc
|
66
70
|
Matthew Manning
|
67
71
|
Michael Klett
|
68
72
|
Michael Rykov
|
@@ -70,12 +74,14 @@ Michael Westbom
|
|
70
74
|
Mike Fletcher
|
71
75
|
Nelson Elhage
|
72
76
|
Nicholas Wieland
|
77
|
+
Nick Hammond
|
73
78
|
Nick Plante
|
74
79
|
Niko Dittmann
|
75
80
|
Oscar Del Ben
|
76
81
|
Pablo Astigarraga
|
77
82
|
Paul Dlug
|
78
83
|
Pedro Belo
|
84
|
+
Pedro Chambino
|
79
85
|
Philip Corliss
|
80
86
|
Pierre-Louis Gottfrois
|
81
87
|
Rafael Ssouza
|
data/README.md
ADDED
@@ -0,0 +1,578 @@
|
|
1
|
+
# REST Client -- simple DSL for accessing HTTP and REST resources
|
2
|
+
|
3
|
+
[![Gem Downloads](https://img.shields.io/gem/dt/rails.svg)](https://rubygems.org/gems/rest-client)
|
4
|
+
[![Build Status](https://travis-ci.org/rest-client/rest-client.svg?branch=master)](https://travis-ci.org/rest-client/rest-client)
|
5
|
+
[![Code Climate](https://codeclimate.com/github/rest-client/rest-client.svg)](https://codeclimate.com/github/rest-client/rest-client)
|
6
|
+
[![Inline docs](http://inch-ci.org/github/rest-client/rest-client.svg?branch=master)](http://www.rubydoc.info/github/rest-client/rest-client/master)
|
7
|
+
|
8
|
+
A simple HTTP and REST client for Ruby, inspired by the Sinatra's microframework style
|
9
|
+
of specifying actions: get, put, post, delete.
|
10
|
+
|
11
|
+
* Main page: https://github.com/rest-client/rest-client
|
12
|
+
* Mailing list: https://groups.io/g/rest-client
|
13
|
+
|
14
|
+
### New mailing list
|
15
|
+
|
16
|
+
We have a new email list for announcements, hosted by Groups.io.
|
17
|
+
|
18
|
+
* Subscribe on the web: https://groups.io/g/rest-client
|
19
|
+
|
20
|
+
* Subscribe by sending an email: mailto:rest-client+subscribe@groups.io
|
21
|
+
|
22
|
+
* Open discussion subgroup: https://groups.io/g/rest-client+discuss
|
23
|
+
|
24
|
+
The old Librelist mailing list is *defunct*, as Librelist appears to be broken
|
25
|
+
and not accepting new mail. The old archives are still up, but have been
|
26
|
+
imported into the new list archives as well.
|
27
|
+
http://librelist.com/browser/rest.client
|
28
|
+
|
29
|
+
## Requirements
|
30
|
+
|
31
|
+
MRI Ruby 2.0 and newer are supported. Alternative interpreters compatible with
|
32
|
+
2.0+ should work as well.
|
33
|
+
|
34
|
+
Earlier Ruby versions such as 1.8.7, 1.9.2, and 1.9.3 are no longer supported. These
|
35
|
+
versions no longer have any official support, and do not receive security
|
36
|
+
updates.
|
37
|
+
|
38
|
+
The rest-client gem depends on these other gems for usage at runtime:
|
39
|
+
|
40
|
+
* [mime-types](http://rubygems.org/gems/mime-types)
|
41
|
+
* [netrc](http://rubygems.org/gems/netrc)
|
42
|
+
* [http-cookie](https://rubygems.org/gems/http-cookie)
|
43
|
+
|
44
|
+
There are also several development dependencies. It's recommended to use
|
45
|
+
[bundler](http://bundler.io/) to manage these dependencies for hacking on
|
46
|
+
rest-client.
|
47
|
+
|
48
|
+
## Usage: Raw URL
|
49
|
+
```ruby
|
50
|
+
require 'rest-client'
|
51
|
+
|
52
|
+
RestClient.get 'http://example.com/resource'
|
53
|
+
|
54
|
+
RestClient.get 'http://example.com/resource', {:params => {:id => 50, 'foo' => 'bar'}}
|
55
|
+
|
56
|
+
RestClient.get 'https://user:password@example.com/private/resource', {:accept => :json}
|
57
|
+
|
58
|
+
RestClient.post 'http://example.com/resource', :param1 => 'one', :nested => { :param2 => 'two' }
|
59
|
+
|
60
|
+
RestClient.post "http://example.com/resource", { 'x' => 1 }.to_json, :content_type => :json, :accept => :json
|
61
|
+
|
62
|
+
RestClient.delete 'http://example.com/resource'
|
63
|
+
|
64
|
+
response = RestClient.get 'http://example.com/resource'
|
65
|
+
response.code
|
66
|
+
➔ 200
|
67
|
+
response.cookies
|
68
|
+
➔ {"Foo"=>"BAR", "QUUX"=>"QUUUUX"}
|
69
|
+
response.headers
|
70
|
+
➔ {:content_type=>"text/html; charset=utf-8", :cache_control=>"private" ...
|
71
|
+
response.to_str
|
72
|
+
➔ \n<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n \"http://www.w3.org/TR/html4/strict.dtd\">\n\n<html ....
|
73
|
+
|
74
|
+
RestClient.post( url,
|
75
|
+
{
|
76
|
+
:transfer => {
|
77
|
+
:path => '/foo/bar',
|
78
|
+
:owner => 'that_guy',
|
79
|
+
:group => 'those_guys'
|
80
|
+
},
|
81
|
+
:upload => {
|
82
|
+
:file => File.new(path, 'rb')
|
83
|
+
}
|
84
|
+
})
|
85
|
+
```
|
86
|
+
## Passing advanced options
|
87
|
+
|
88
|
+
The top level helper methods like RestClient.get accept a headers hash as
|
89
|
+
their last argument and don't allow passing more complex options. But these
|
90
|
+
helpers are just thin wrappers around `RestClient::Request.execute`.
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
RestClient::Request.execute(method: :get, url: 'http://example.com/resource',
|
94
|
+
timeout: 10)
|
95
|
+
|
96
|
+
RestClient::Request.execute(method: :get, url: 'http://example.com/resource',
|
97
|
+
ssl_ca_file: 'myca.pem',
|
98
|
+
ssl_ciphers: 'AESGCM:!aNULL')
|
99
|
+
```
|
100
|
+
You can also use this to pass a payload for HTTP verbs like DELETE, where the
|
101
|
+
`RestClient.delete` helper doesn't accept a payload.
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
RestClient::Request.execute(method: :delete, url: 'http://example.com/resource',
|
105
|
+
payload: 'foo', headers: {myheader: 'bar'})
|
106
|
+
```
|
107
|
+
|
108
|
+
Due to unfortunate choices in the original API, the params used to populate the
|
109
|
+
query string are actually taken out of the headers hash. So if you want to pass
|
110
|
+
both the params hash and more complex options, use the special key
|
111
|
+
`:params` in the headers hash. This design may change in a future major
|
112
|
+
release.
|
113
|
+
|
114
|
+
```ruby
|
115
|
+
RestClient::Request.execute(method: :get, url: 'http://example.com/resource',
|
116
|
+
timeout: 10, headers: {params: {foo: 'bar'}})
|
117
|
+
|
118
|
+
➔ GET http://example.com/resource?foo=bar
|
119
|
+
```
|
120
|
+
|
121
|
+
## Multipart
|
122
|
+
|
123
|
+
Yeah, that's right! This does multipart sends for you!
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
RestClient.post '/data', :myfile => File.new("/path/to/image.jpg", 'rb')
|
127
|
+
```
|
128
|
+
|
129
|
+
This does two things for you:
|
130
|
+
|
131
|
+
- Auto-detects that you have a File value sends it as multipart
|
132
|
+
- Auto-detects the mime of the file and sets it in the HEAD of the payload for each entry
|
133
|
+
|
134
|
+
If you are sending params that do not contain a File object but the payload needs to be multipart then:
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
RestClient.post '/data', {:foo => 'bar', :multipart => true}
|
138
|
+
```
|
139
|
+
|
140
|
+
## Usage: ActiveResource-Style
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
resource = RestClient::Resource.new 'http://example.com/resource'
|
144
|
+
resource.get
|
145
|
+
|
146
|
+
private_resource = RestClient::Resource.new 'https://example.com/private/resource', 'user', 'pass'
|
147
|
+
private_resource.put File.read('pic.jpg'), :content_type => 'image/jpg'
|
148
|
+
```
|
149
|
+
|
150
|
+
See RestClient::Resource module docs for details.
|
151
|
+
|
152
|
+
## Usage: Resource Nesting
|
153
|
+
|
154
|
+
```ruby
|
155
|
+
site = RestClient::Resource.new('http://example.com')
|
156
|
+
site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
157
|
+
```
|
158
|
+
See `RestClient::Resource` docs for details.
|
159
|
+
|
160
|
+
## Exceptions (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
|
161
|
+
|
162
|
+
- for result codes between `200` and `207`, a `RestClient::Response` will be returned
|
163
|
+
- for result codes `301`, `302` or `307`, the redirection will be followed if the request is a `GET` or a `HEAD`
|
164
|
+
- for result code `303`, the redirection will be followed and the request transformed into a `GET`
|
165
|
+
- for other cases, a `RestClient::Exception` holding the Response will be raised; a specific exception class will be thrown for known error codes
|
166
|
+
- call `.response` on the exception to get the server's response
|
167
|
+
|
168
|
+
```ruby
|
169
|
+
RestClient.get 'http://example.com/resource'
|
170
|
+
➔ RestClient::ResourceNotFound: RestClient::ResourceNotFound
|
171
|
+
|
172
|
+
begin
|
173
|
+
RestClient.get 'http://example.com/resource'
|
174
|
+
rescue => e
|
175
|
+
e.response
|
176
|
+
end
|
177
|
+
➔ 404 Resource Not Found | text/html 282 bytes
|
178
|
+
```
|
179
|
+
|
180
|
+
## Result handling
|
181
|
+
|
182
|
+
The result of a `RestClient::Request` is a `RestClient::Response` object.
|
183
|
+
|
184
|
+
__New in 2.0:__ `RestClient::Response` objects are now a subclass of `String`.
|
185
|
+
Previously, they were a real String object with response functionality mixed
|
186
|
+
in, which was very confusing to work with.
|
187
|
+
|
188
|
+
Response objects have several useful methods. (See the class rdoc for more details.)
|
189
|
+
|
190
|
+
- `Response#code`: The HTTP response code
|
191
|
+
- `Response#body`: The response body as a string. (AKA .to_s)
|
192
|
+
- `Response#headers`: A hash of HTTP response headers
|
193
|
+
- `Response#raw_headers`: A hash of HTTP response headers as unprocessed arrays
|
194
|
+
- `Response#cookies`: A hash of HTTP cookies set by the server
|
195
|
+
- `Response#cookie_jar`: <em>New in 1.8</em> An HTTP::CookieJar of cookies
|
196
|
+
- `Response#request`: The RestClient::Request object used to make the request
|
197
|
+
- `Response#history`: If redirection was followed, a list of prior Response objects
|
198
|
+
|
199
|
+
```ruby
|
200
|
+
RestClient.get('http://example.com')
|
201
|
+
➔ <RestClient::Response 200 "<!doctype h...">
|
202
|
+
|
203
|
+
begin
|
204
|
+
RestClient.get('http://example.com/notfound')
|
205
|
+
rescue RestClient::ExceptionWithResponse => err
|
206
|
+
err.response
|
207
|
+
end
|
208
|
+
➔ <RestClient::Response 404 "<!doctype h...">
|
209
|
+
```
|
210
|
+
|
211
|
+
### Response callbacks
|
212
|
+
|
213
|
+
A block can be passed to the RestClient method. This block will then be called with the Response.
|
214
|
+
Response.return! can be called to invoke the default response's behavior.
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
# Don't raise exceptions but return the response
|
218
|
+
RestClient.get('http://example.com/resource'){|response, request, result| response }
|
219
|
+
➔ 404 Resource Not Found | text/html 282 bytes
|
220
|
+
|
221
|
+
# Manage a specific error code
|
222
|
+
RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
|
223
|
+
case response.code
|
224
|
+
when 200
|
225
|
+
p "It worked !"
|
226
|
+
response
|
227
|
+
when 423
|
228
|
+
raise SomeCustomExceptionIfYouWant
|
229
|
+
else
|
230
|
+
response.return!(request, result, &block)
|
231
|
+
end
|
232
|
+
}
|
233
|
+
|
234
|
+
# Follow redirections for all request types and not only for get and head
|
235
|
+
# RFC : "If the 301, 302 or 307 status code is received in response to a request other than GET or HEAD,
|
236
|
+
# the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user,
|
237
|
+
# since this might change the conditions under which the request was issued."
|
238
|
+
RestClient.get('http://my-rest-service.com/resource'){ |response, request, result, &block|
|
239
|
+
if [301, 302, 307].include? response.code
|
240
|
+
response.follow_redirection(request, result, &block)
|
241
|
+
else
|
242
|
+
response.return!(request, result, &block)
|
243
|
+
end
|
244
|
+
}
|
245
|
+
```
|
246
|
+
## Non-normalized URIs
|
247
|
+
|
248
|
+
If you need to normalize URIs, e.g. to work with International Resource Identifiers (IRIs),
|
249
|
+
use the addressable gem (http://addressable.rubyforge.org/api/) in your code:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
require 'addressable/uri'
|
253
|
+
RestClient.get(Addressable::URI.parse("http://www.詹姆斯.com/").normalize.to_str)
|
254
|
+
```
|
255
|
+
|
256
|
+
## Lower-level access
|
257
|
+
|
258
|
+
For cases not covered by the general API, you can use the `RestClient::Request` class, which provides a lower-level API.
|
259
|
+
|
260
|
+
You can:
|
261
|
+
|
262
|
+
- specify ssl parameters
|
263
|
+
- override cookies
|
264
|
+
- manually handle the response (e.g. to operate on it as a stream rather than reading it all into memory)
|
265
|
+
|
266
|
+
See `RestClient::Request`'s documentation for more information.
|
267
|
+
|
268
|
+
## Shell
|
269
|
+
|
270
|
+
The restclient shell command gives an IRB session with RestClient already loaded:
|
271
|
+
|
272
|
+
```ruby
|
273
|
+
$ restclient
|
274
|
+
>> RestClient.get 'http://example.com'
|
275
|
+
```
|
276
|
+
|
277
|
+
Specify a URL argument for get/post/put/delete on that resource:
|
278
|
+
|
279
|
+
```ruby
|
280
|
+
$ restclient http://example.com
|
281
|
+
>> put '/resource', 'data'
|
282
|
+
```
|
283
|
+
|
284
|
+
Add a user and password for authenticated resources:
|
285
|
+
|
286
|
+
```ruby
|
287
|
+
$ restclient https://example.com user pass
|
288
|
+
>> delete '/private/resource'
|
289
|
+
```
|
290
|
+
|
291
|
+
Create ~/.restclient for named sessions:
|
292
|
+
|
293
|
+
```ruby
|
294
|
+
sinatra:
|
295
|
+
url: http://localhost:4567
|
296
|
+
rack:
|
297
|
+
url: http://localhost:9292
|
298
|
+
private_site:
|
299
|
+
url: http://example.com
|
300
|
+
username: user
|
301
|
+
password: pass
|
302
|
+
```
|
303
|
+
|
304
|
+
Then invoke:
|
305
|
+
|
306
|
+
```ruby
|
307
|
+
$ restclient private_site
|
308
|
+
```
|
309
|
+
|
310
|
+
Use as a one-off, curl-style:
|
311
|
+
|
312
|
+
```ruby
|
313
|
+
$ restclient get http://example.com/resource > output_body
|
314
|
+
|
315
|
+
$ restclient put http://example.com/resource < input_body
|
316
|
+
```
|
317
|
+
|
318
|
+
## Logging
|
319
|
+
|
320
|
+
To enable logging you can:
|
321
|
+
|
322
|
+
- set RestClient.log with a Ruby Logger, or
|
323
|
+
- set an environment variable to avoid modifying the code (in this case you can use a file name, "stdout" or "stderr"):
|
324
|
+
|
325
|
+
```ruby
|
326
|
+
$ RESTCLIENT_LOG=stdout path/to/my/program
|
327
|
+
```
|
328
|
+
Either produces logs like this:
|
329
|
+
|
330
|
+
```ruby
|
331
|
+
RestClient.get "http://some/resource"
|
332
|
+
# => 200 OK | text/html 250 bytes
|
333
|
+
RestClient.put "http://some/resource", "payload"
|
334
|
+
# => 401 Unauthorized | application/xml 340 bytes
|
335
|
+
```
|
336
|
+
|
337
|
+
Note that these logs are valid Ruby, so you can paste them into the `restclient`
|
338
|
+
shell or a script to replay your sequence of rest calls.
|
339
|
+
|
340
|
+
## Proxy
|
341
|
+
|
342
|
+
All calls to RestClient, including Resources, will use the proxy specified by
|
343
|
+
`RestClient.proxy`:
|
344
|
+
|
345
|
+
```ruby
|
346
|
+
RestClient.proxy = "http://proxy.example.com/"
|
347
|
+
RestClient.get "http://some/resource"
|
348
|
+
# => response from some/resource as proxied through proxy.example.com
|
349
|
+
```
|
350
|
+
|
351
|
+
Often the proxy URL is set in an environment variable, so you can do this to
|
352
|
+
use whatever proxy the system is configured to use:
|
353
|
+
|
354
|
+
```ruby
|
355
|
+
RestClient.proxy = ENV['http_proxy']
|
356
|
+
```
|
357
|
+
|
358
|
+
__New in 2.0:__ Specify a per-request proxy by passing the :proxy option to
|
359
|
+
RestClient::Request. This will override any proxies set by environment variable
|
360
|
+
or by the global `RestClient.proxy` value.
|
361
|
+
|
362
|
+
```ruby
|
363
|
+
RestClient::Request.execute(method: :get, url: 'http://example.com',
|
364
|
+
proxy: 'http://proxy.example.com')
|
365
|
+
# => single request proxied through the proxy
|
366
|
+
```
|
367
|
+
|
368
|
+
This can be used to disable the use of a proxy for a particular request.
|
369
|
+
|
370
|
+
```ruby
|
371
|
+
RestClient.proxy = "http://proxy.example.com/"
|
372
|
+
RestClient::Request.execute(method: :get, url: 'http://example.com', proxy: nil)
|
373
|
+
# => single request sent without a proxy
|
374
|
+
```
|
375
|
+
|
376
|
+
## Query parameters
|
377
|
+
|
378
|
+
Rest-client can render a hash as HTTP query parameters for GET/HEAD/DELETE
|
379
|
+
requests or as HTTP post data in `x-www-form-urlencoded` format for POST
|
380
|
+
requests.
|
381
|
+
|
382
|
+
__New in 2.0:__ Even though there is no standard specifying how this should
|
383
|
+
work, rest-client follows a similar convention to the one used by Rack / Rails
|
384
|
+
servers for handling arrays, nested hashes, and null values.
|
385
|
+
|
386
|
+
The implementation in
|
387
|
+
[./lib/rest-client/utils.rb](RestClient::Utils.encode_query_string)
|
388
|
+
closely follows
|
389
|
+
[Rack::Utils.build_nested_query](http://www.rubydoc.info/gems/rack/Rack/Utils#build_nested_query-class_method),
|
390
|
+
but treats empty arrays and hashes as `nil`. (Rack drops them entirely, which
|
391
|
+
is confusing behavior.)
|
392
|
+
|
393
|
+
If you don't like this behavior and want more control, just serialize params
|
394
|
+
yourself (e.g. with `URI.encode_www_form`) and add the query string to the URL
|
395
|
+
directly for GET parameters or pass the payload as a string for POST requests.
|
396
|
+
|
397
|
+
Basic GET params:
|
398
|
+
```ruby
|
399
|
+
RestClient.get('https://httpbin.org/get', params: {foo: 'bar', baz: 'qux'})
|
400
|
+
# GET "https://httpbin.org/get?foo=bar&baz=qux"
|
401
|
+
```
|
402
|
+
|
403
|
+
Basic `x-www-form-urlencoded` POST params:
|
404
|
+
```ruby
|
405
|
+
>> r = RestClient.post('https://httpbin.org/post', {foo: 'bar', baz: 'qux'})
|
406
|
+
# POST "https://httpbin.org/post", data: "foo=bar&baz=qux"
|
407
|
+
=> <RestClient::Response 200 "{\n \"args\":...">
|
408
|
+
>> JSON.parse(r.body)
|
409
|
+
=> {"args"=>{},
|
410
|
+
"data"=>"",
|
411
|
+
"files"=>{},
|
412
|
+
"form"=>{"baz"=>"qux", "foo"=>"bar"},
|
413
|
+
"headers"=>
|
414
|
+
{"Accept"=>"*/*",
|
415
|
+
"Accept-Encoding"=>"gzip, deflate",
|
416
|
+
"Content-Length"=>"15",
|
417
|
+
"Content-Type"=>"application/x-www-form-urlencoded",
|
418
|
+
"Host"=>"httpbin.org"},
|
419
|
+
"json"=>nil,
|
420
|
+
"url"=>"https://httpbin.org/post"}
|
421
|
+
```
|
422
|
+
|
423
|
+
Advanced GET params (arrays):
|
424
|
+
```ruby
|
425
|
+
>> r = RestClient.get('https://http-params.herokuapp.com/get', params: {foo: [1,2,3]})
|
426
|
+
# GET "https://http-params.herokuapp.com/get?foo[]=1&foo[]=2&foo[]=3"
|
427
|
+
=> <RestClient::Response 200 "Method: GET...">
|
428
|
+
>> puts r.body
|
429
|
+
query_string: "foo[]=1&foo[]=2&foo[]=3"
|
430
|
+
decoded: "foo[]=1&foo[]=2&foo[]=3"
|
431
|
+
|
432
|
+
GET:
|
433
|
+
{"foo"=>["1", "2", "3"]}
|
434
|
+
```
|
435
|
+
|
436
|
+
Advanced GET params (nested hashes):
|
437
|
+
```ruby
|
438
|
+
>> r = RestClient.get('https://http-params.herokuapp.com/get', params: {outer: {foo: 123, bar: 456}})
|
439
|
+
# GET "https://http-params.herokuapp.com/get?outer[foo]=123&outer[bar]=456"
|
440
|
+
=> <RestClient::Response 200 "Method: GET...">
|
441
|
+
>> puts r.body
|
442
|
+
...
|
443
|
+
query_string: "outer[foo]=123&outer[bar]=456"
|
444
|
+
decoded: "outer[foo]=123&outer[bar]=456"
|
445
|
+
|
446
|
+
GET:
|
447
|
+
{"outer"=>{"foo"=>"123", "bar"=>"456"}}
|
448
|
+
```
|
449
|
+
|
450
|
+
__New in 2.0:__ The new `RestClient::ParamsArray` class allows callers to
|
451
|
+
provide ordering even to structured parameters. This is useful for unusual
|
452
|
+
cases where the server treats the order of parameters as significant or you
|
453
|
+
want to pass a particular key multiple times.
|
454
|
+
|
455
|
+
Multiple fields with the same name using ParamsArray:
|
456
|
+
```ruby
|
457
|
+
>> RestClient.get('https://httpbin.org/get', params:
|
458
|
+
RestClient::ParamsArray.new([[:foo, 1], [:foo, 2]]))
|
459
|
+
# GET "https://httpbin.org/get?foo=1&foo=2"
|
460
|
+
```
|
461
|
+
|
462
|
+
Nested ParamsArray:
|
463
|
+
```ruby
|
464
|
+
>> RestClient.get('https://httpbin.org/get', params:
|
465
|
+
{foo: RestClient::ParamsArray.new([[:a, 1], [:a, 2]])})
|
466
|
+
# GET "https://httpbin.org/get?foo[a]=1&foo[a]=2"
|
467
|
+
```
|
468
|
+
|
469
|
+
## Headers
|
470
|
+
|
471
|
+
Request headers can be set by passing a ruby hash containing keys and values
|
472
|
+
representing header names and values:
|
473
|
+
|
474
|
+
```ruby
|
475
|
+
# GET request with modified headers
|
476
|
+
RestClient.get 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
|
477
|
+
|
478
|
+
# POST request with modified headers
|
479
|
+
RestClient.post 'http://example.com/resource', {:foo => 'bar', :baz => 'qux'}, {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
|
480
|
+
|
481
|
+
# DELETE request with modified headers
|
482
|
+
RestClient.delete 'http://example.com/resource', {:Authorization => 'Bearer cT0febFoD5lxAlNAXHo6g'}
|
483
|
+
```
|
484
|
+
|
485
|
+
## Cookies
|
486
|
+
|
487
|
+
Request and Response objects know about HTTP cookies, and will automatically
|
488
|
+
extract and set headers for them as needed:
|
489
|
+
|
490
|
+
```ruby
|
491
|
+
response = RestClient.get 'http://example.com/action_which_sets_session_id'
|
492
|
+
response.cookies
|
493
|
+
# => {"_applicatioN_session_id" => "1234"}
|
494
|
+
|
495
|
+
response2 = RestClient.post(
|
496
|
+
'http://localhost:3000/',
|
497
|
+
{:param1 => "foo"},
|
498
|
+
{:cookies => {:session_id => "1234"}}
|
499
|
+
)
|
500
|
+
# ...response body
|
501
|
+
```
|
502
|
+
### Full cookie jar support (new in 1.8)
|
503
|
+
|
504
|
+
The original cookie implementation was very naive and ignored most of the
|
505
|
+
cookie RFC standards.
|
506
|
+
__New in 1.8__: An HTTP::CookieJar of cookies
|
507
|
+
|
508
|
+
Response objects now carry a cookie_jar method that exposes an HTTP::CookieJar
|
509
|
+
of cookies, which supports full standards compliant behavior.
|
510
|
+
|
511
|
+
## SSL/TLS support
|
512
|
+
|
513
|
+
Various options are supported for configuring rest-client's TLS settings. By
|
514
|
+
default, rest-client will verify certificates using the system's CA store on
|
515
|
+
all platforms. (This is intended to be similar to how browsers behave.) You can
|
516
|
+
specify an :ssl_ca_file, :ssl_ca_path, or :ssl_cert_store to customize the
|
517
|
+
certificate authorities accepted.
|
518
|
+
|
519
|
+
### SSL Client Certificates
|
520
|
+
|
521
|
+
```ruby
|
522
|
+
RestClient::Resource.new(
|
523
|
+
'https://example.com',
|
524
|
+
:ssl_client_cert => OpenSSL::X509::Certificate.new(File.read("cert.pem")),
|
525
|
+
:ssl_client_key => OpenSSL::PKey::RSA.new(File.read("key.pem"), "passphrase, if any"),
|
526
|
+
:ssl_ca_file => "ca_certificate.pem",
|
527
|
+
:verify_ssl => OpenSSL::SSL::VERIFY_PEER
|
528
|
+
).get
|
529
|
+
```
|
530
|
+
Self-signed certificates can be generated with the openssl command-line tool.
|
531
|
+
|
532
|
+
## Hook
|
533
|
+
|
534
|
+
RestClient.add_before_execution_proc add a Proc to be called before each execution.
|
535
|
+
It's handy if you need direct access to the HTTP request.
|
536
|
+
|
537
|
+
Example:
|
538
|
+
|
539
|
+
```ruby
|
540
|
+
# Add oauth support using the oauth gem
|
541
|
+
require 'oauth'
|
542
|
+
access_token = ...
|
543
|
+
|
544
|
+
RestClient.add_before_execution_proc do |req, params|
|
545
|
+
access_token.sign! req
|
546
|
+
end
|
547
|
+
|
548
|
+
RestClient.get 'http://example.com'
|
549
|
+
```
|
550
|
+
|
551
|
+
## More
|
552
|
+
|
553
|
+
Need caching, more advanced logging or any ability provided by Rack middleware?
|
554
|
+
|
555
|
+
Have a look at rest-client-components: http://github.com/crohr/rest-client-components
|
556
|
+
|
557
|
+
## Credits
|
558
|
+
|
559
|
+
|||
|
560
|
+
|---------------------|---------------------------------------------------------|
|
561
|
+
| REST Client Team | Andy Brody |
|
562
|
+
| Creator | Adam Wiggins |
|
563
|
+
| Maintainers Emeriti | Lawrence Leonard Gilbert, Matthew Manning, Julien Kirch |
|
564
|
+
| Major contributions | Blake Mizerany, Julien Kirch |
|
565
|
+
|
566
|
+
A great many generous folks have contributed features and patches.
|
567
|
+
See AUTHORS for the full list.
|
568
|
+
|
569
|
+
## Legal
|
570
|
+
|
571
|
+
Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
|
572
|
+
|
573
|
+
"Master Shake" photo (http://www.flickr.com/photos/solgrundy/924205581/) by
|
574
|
+
"SolGrundy"; used under terms of the Creative Commons Attribution-ShareAlike 2.0
|
575
|
+
Generic license (http://creativecommons.org/licenses/by-sa/2.0/)
|
576
|
+
|
577
|
+
Code for reading Windows root certificate store derived from work by Puppet;
|
578
|
+
used under terms of the Apache License, Version 2.0.
|