@@ -39,6 +39,10 @@ class HTTPHeaderSyntaxError < StandardError; end
39
39
# - {Hypertext Transfer Protocol}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol].
40
40
# - {Technical overview}[https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Technical_overview].
41
41
#
42
+ # == About the Examples
43
+ #
44
+ # :include: doc/net-http/examples.rdoc
45
+ #
42
46
# == Strategies
43
47
#
44
48
# - If you will make only a few GET requests,
@@ -49,17 +53,19 @@ class HTTPHeaderSyntaxError < StandardError; end
49
53
# a {session}[rdoc-ref:Net::HTTP@Sessions] that sends a single request:
50
54
#
51
55
# # Return string response body.
52
- # Net::HTTP.get(hostname, path, port = 80 )
53
- # Net::HTTP.get(uri, headers = {}, port = 80 )
56
+ # Net::HTTP.get(hostname, path)
57
+ # Net::HTTP.get(uri)
54
58
#
55
59
# # Write string response body to $stdout.
56
- # Net::HTTP.get_print(hostname, path_or_uri, port = 80 )
57
- # Net::HTTP.get_print(uri, headers = {}, port = 80 )
60
+ # Net::HTTP.get_print(hostname, path )
61
+ # Net::HTTP.get_print(uri)
58
62
#
59
63
# # Return response as Net::HTTPResponse object.
60
- # Net::HTTP.get_response(hostname, path_or_uri, port = 80)
61
- # Net::HTTP.get_response(uri, headers = {}, port = 80)
62
- # Net::HTTP.post(uri, data, headers = {})
64
+ # Net::HTTP.get_response(hostname, path)
65
+ # Net::HTTP.get_response(uri)
66
+ # data = '{"title": "foo", "body": "bar", "userId": 1}'
67
+ # Net::HTTP.post(uri, data)
68
+ # params = {title: 'foo', body: 'bar', userId: 1}
63
69
# Net::HTTP.post_form(uri, params)
64
70
#
65
71
# - If performance is important, consider using sessions, which lower request overhead.
@@ -69,32 +75,29 @@ class HTTPHeaderSyntaxError < StandardError; end
69
75
#
70
76
# Net::HTTP.start(hostname) do |http|
71
77
# # Session started automatically before block execution.
72
- # http.get(path_or_uri, headers = {})
73
- # http.head(path_or_uri, headers = {})
74
- # http.post(path_or_uri, body, headers = {}) # Can also have a block.
75
- # http.put(path_or_uri, body, headers = {})
76
- # http.delete(path_or_uri, headers = {Depth: 'Infinity'})
77
- # http.options(path_or_uri, headers = {})
78
- # http.trace(path_or_uri, headers = {})
79
- # http.patch(path_or_uri, body, headers = {}) # Can also have a block.
80
- # http.copy(path_or_uri, headers = {})
81
- # http.lock(path_or_uri, body, headers = {})
82
- # http.mkcol(path_or_uri, body = nil, headers = {})
83
- # http.move(path_or_uri, headers = {})
84
- # http.propfind(path_or_uri, body = nil, headers = {'Depth' => '0'})
85
- # http.proppatch(path_or_uri, body, headers = {})
86
- # http.unlock(path_or_uri, body, headers = {})
78
+ # http.get(path)
79
+ # http.head(path)
80
+ # body = 'Some text'
81
+ # http.post(path, body) # Can also have a block.
82
+ # http.put(path, body)
83
+ # http.delete(path)
84
+ # http.options(path)
85
+ # http.trace(path)
86
+ # http.patch(path, body) # Can also have a block.
87
+ # http.copy(path)
88
+ # http.lock(path, body)
89
+ # http.mkcol(path, body)
90
+ # http.move(path)
91
+ # http.propfind(path, body)
92
+ # http.proppatch(path, body)
93
+ # http.unlock(path, body)
87
94
# # Session finished automatically at block exit.
88
95
# end
89
96
#
90
97
# The methods cited above are convenience methods that, via their few arguments,
91
98
# allow minimal control over the requests.
92
99
# For greater control, consider using {request objects}[rdoc-ref:Net::HTTPRequest].
93
100
#
94
- # == About the Examples
95
- #
96
- # :include: doc/net-http/examples.rdoc
97
- #
98
101
# == URIs
99
102
#
100
103
# On the internet, a URI
@@ -263,91 +266,77 @@ class HTTPHeaderSyntaxError < StandardError; end
263
266
#
264
267
# For example, all 2XX responses are instances of a Net::HTTPSuccess
265
268
# subclass, a 3XX response is an instance of a Net::HTTPRedirection
266
- # subclass and a 200 response is an instance of the Net::HTTPOK class. For
267
- # details of response classes, see the section "HTTP Response Classes"
268
- # below.
269
+ # subclass and a 200 response is an instance of the Net::HTTPOK class.
270
+ # For details, see HTTPResponse.
269
271
#
270
272
# Using a case statement you can handle various types of responses properly:
271
273
#
272
- # def fetch(uri_str , limit = 10)
274
+ # def fetch(uri , limit = 10)
273
275
# # You should choose a better exception.
274
276
# raise ArgumentError, 'too many HTTP redirects' if limit == 0
275
277
#
276
- # response = Net::HTTP.get_response(URI(uri_str))
277
- #
278
- # case response
278
+ # res = Net::HTTP.get_response(URI(uri))
279
+ # case res
279
280
# when Net::HTTPSuccess then
280
- # response
281
+ # res
281
282
# when Net::HTTPRedirection then
282
- # location = response ['location']
283
+ # location = res ['location']
283
284
# warn "redirected to #{location}"
284
285
# fetch(location, limit - 1)
285
286
# else
286
- # response .value
287
+ # res .value
287
288
# end
288
289
# end
289
290
#
290
- # print fetch('http://www.ruby-lang.org' )
291
+ # fetch(uri )
291
292
#
292
293
# == Basic Authentication
293
294
#
294
295
# Basic authentication is performed according to
295
- # [RFC2617](http://www.ietf.org/rfc/rfc2617.txt).
296
- #
297
- # uri = URI('http://example.com/index.html?key=value')
296
+ # {RFC2617}[http://www.ietf.org/rfc/rfc2617.txt]:
298
297
#
299
298
# req = Net::HTTP::Get.new(uri)
300
- # req.basic_auth 'user', 'pass'
301
- #
302
- # res = Net::HTTP.start(uri.hostname, uri.port) {|http|
299
+ # req.basic_auth('user', 'pass')
300
+ # res = Net::HTTP.start(hostname) do |http|
303
301
# http.request(req)
304
- # }
305
- # puts res.body
302
+ # end
306
303
#
307
304
# == Streaming Response Bodies
308
305
#
309
- # By default Net::HTTP reads an entire response into memory. If you are
306
+ # By default \ Net::HTTP reads an entire response into memory. If you are
310
307
# handling large files or wish to implement a progress bar you can instead
311
308
# stream the body directly to an IO.
312
309
#
313
- # uri = URI('http://example.com/large_file')
314
- #
315
- # Net::HTTP.start(uri.host, uri.port) do |http|
316
- # request = Net::HTTP::Get.new uri
317
- #
318
- # http.request request do |response|
319
- # open 'large_file', 'w' do |io|
320
- # response.read_body do |chunk|
321
- # io.write chunk
310
+ # Net::HTTP.start(hostname) do |http|
311
+ # req = Net::HTTP::Get.new(uri)
312
+ # http.request(req) do |res|
313
+ # open('t.tmp', 'w') do |f|
314
+ # res.read_body do |chunk|
315
+ # f.write chunk
322
316
# end
323
317
# end
324
318
# end
325
319
# end
326
320
#
327
321
# == HTTPS
328
322
#
329
- # HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=.
330
- #
331
- # uri = URI('https://secure.example.com/some_path?query=string')
323
+ # HTTPS is enabled for an HTTP connection by Net::HTTP#use_ssl=:
332
324
#
333
- # Net::HTTP.start(uri.host, uri.port , :use_ssl => true) do |http|
334
- # request = Net::HTTP::Get.new uri
335
- # response = http.request request # Net::HTTPResponse object
325
+ # Net::HTTP.start(hostname , :use_ssl => true) do |http|
326
+ # req = Net::HTTP::Get.new( uri)
327
+ # res = http.request(req)
336
328
# end
337
329
#
338
- # Or if you simply want to make a GET request, you may pass in an URI
339
- # object that has an HTTPS URL. Net::HTTP automatically turns on TLS
340
- # verification if the URI object has a 'https' URI scheme.
330
+ # Or if you simply want to make a GET request, you may pass in a URI
331
+ # object that has an HTTPS URL. \ Net::HTTP automatically turns on TLS
332
+ # verification if the URI object has a 'https' URI scheme:
341
333
#
342
- # uri = URI('https://example.com/')
343
- # Net::HTTP.get(uri) # => String
344
- #
345
- # In previous versions of Ruby you would need to require 'net/https' to use
346
- # HTTPS. This is no longer true.
334
+ # uri # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
335
+ # Net::HTTP.get(uri)
347
336
#
348
337
# == Proxies
349
338
#
350
- # Net::HTTP will automatically create a proxy from the +http_proxy+
339
+ # \ Net::HTTP will automatically create a proxy from the +http_proxy+
351
340
# environment variable if it is present. To disable use of +http_proxy+,
352
341
# pass +nil+ for the proxy address.
353
342
#
@@ -360,12 +349,9 @@ class HTTPHeaderSyntaxError < StandardError; end
360
349
# # always proxy via your.proxy.addr:8080
361
350
# }
362
351
#
363
- # See Net::HTTP.new for further details and examples such as proxies that
364
- # require a username and password.
365
- #
366
352
# == Compression
367
353
#
368
- # Net::HTTP automatically adds Accept-Encoding for compression of response
354
+ # \ Net::HTTP automatically adds Accept-Encoding for compression of response
369
355
# bodies and automatically decompresses gzip and deflate responses unless a
370
356
# Range header was sent.
371
357
#
@@ -686,7 +672,7 @@ class << HTTP
686
672
alias newobj new # :nodoc:
687
673
end
688
674
689
- # Returns a new Net::HTTP object +http+
675
+ # Returns a new \ Net::HTTP object +http+
690
676
# (but does not open a TCP connection or HTTP session).
691
677
#
692
678
# <b>No Proxy</b>
@@ -819,7 +805,7 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
819
805
http
820
806
end
821
807
822
- # Creates a new Net::HTTP object for the specified server address,
808
+ # Creates a new \ Net::HTTP object for the specified server address,
823
809
# without opening the TCP connection or initializing the HTTP session.
824
810
# The +address+ should be a DNS hostname or IP address.
825
811
def initialize ( address , port = nil )
@@ -1007,7 +993,7 @@ def ipaddr=(addr)
1007
993
# Number of seconds to wait for the connection to open. Any number
1008
994
# may be used, including Floats for fractional seconds. If the HTTP
1009
995
# object cannot open a connection in this many seconds, it raises a
1010
- # Net::OpenTimeout exception. The default value is 60 seconds.
996
+ # \ Net::OpenTimeout exception. The default value is 60 seconds.
1011
997
attr_accessor :open_timeout
1012
998
1013
999
# Number of seconds to wait for one block to be read (via one read(2)
@@ -1019,12 +1005,12 @@ def ipaddr=(addr)
1019
1005
# Number of seconds to wait for one block to be written (via one write(2)
1020
1006
# call). Any number may be used, including Floats for fractional
1021
1007
# seconds. If the HTTP object cannot write data in this many seconds,
1022
- # it raises a Net::WriteTimeout exception. The default value is 60 seconds.
1023
- # Net::WriteTimeout is not raised on Windows.
1008
+ # it raises a \ Net::WriteTimeout exception. The default value is 60 seconds.
1009
+ # \ Net::WriteTimeout is not raised on Windows.
1024
1010
attr_reader :write_timeout
1025
1011
1026
1012
# Sets the maximum number of times to retry an idempotent request in case of
1027
- # Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
1013
+ # \ Net::ReadTimeout, IOError, EOFError, Errno::ECONNRESET,
1028
1014
# Errno::ECONNABORTED, Errno::EPIPE, OpenSSL::SSL::SSLError,
1029
1015
# Timeout::Error.
1030
1016
# The initial value is 1.
@@ -1084,7 +1070,7 @@ def continue_timeout=(sec)
1084
1070
1085
1071
# Seconds to reuse the connection of the previous request.
1086
1072
# If the idle time is less than this Keep-Alive Timeout,
1087
- # Net::HTTP reuses the TCP/IP socket used by the previous communication.
1073
+ # \ Net::HTTP reuses the TCP/IP socket used by the previous communication.
1088
1074
# The default value is 2 seconds.
1089
1075
attr_accessor :keep_alive_timeout
1090
1076
@@ -1109,7 +1095,7 @@ def use_ssl?
1109
1095
# Turn on/off SSL.
1110
1096
# This flag must be set before starting session.
1111
1097
# If you change use_ssl value after session started,
1112
- # a Net::HTTP object raises IOError .
1098
+ # IOError is raised .
1113
1099
def use_ssl = ( flag )
1114
1100
flag = flag ? true : false
1115
1101
if started? and @use_ssl != flag
@@ -1218,7 +1204,7 @@ def peer_cert
1218
1204
1219
1205
# Opens a TCP connection and HTTP session.
1220
1206
#
1221
- # When this method is called with a block, it passes the Net::HTTP
1207
+ # When this method is called with a block, it passes the \ Net::HTTP
1222
1208
# object to the block, and closes the TCP connection and HTTP session
1223
1209
# after the block has been executed.
1224
1210
#
@@ -1387,11 +1373,11 @@ def do_finish
1387
1373
@proxy_user = nil
1388
1374
@proxy_pass = nil
1389
1375
1390
- # Creates an HTTP proxy class which behaves like Net::HTTP, but
1376
+ # Creates an HTTP proxy class which behaves like \ Net::HTTP, but
1391
1377
# performs all access via the specified proxy.
1392
1378
#
1393
1379
# This class is obsolete. You may pass these same parameters directly to
1394
- # Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1380
+ # \ Net::HTTP.new. See Net::HTTP.new for details of the arguments.
1395
1381
def HTTP . Proxy ( p_addr = :ENV , p_port = nil , p_user = nil , p_pass = nil ) #:nodoc:
1396
1382
return self unless p_addr
1397
1383
@@ -1419,16 +1405,16 @@ def proxy_class?
1419
1405
defined? ( @is_proxy_class ) ? @is_proxy_class : false
1420
1406
end
1421
1407
1422
- # Address of proxy host. If Net::HTTP does not use a proxy, nil.
1408
+ # Address of proxy host. If \ Net::HTTP does not use a proxy, nil.
1423
1409
attr_reader :proxy_address
1424
1410
1425
- # Port number of proxy host. If Net::HTTP does not use a proxy, nil.
1411
+ # Port number of proxy host. If \ Net::HTTP does not use a proxy, nil.
1426
1412
attr_reader :proxy_port
1427
1413
1428
- # User name for accessing proxy. If Net::HTTP does not use a proxy, nil.
1414
+ # User name for accessing proxy. If \ Net::HTTP does not use a proxy, nil.
1429
1415
attr_reader :proxy_user
1430
1416
1431
- # User password for accessing proxy. If Net::HTTP does not use a proxy,
1417
+ # User password for accessing proxy. \ If Net::HTTP does not use a proxy,
1432
1418
# nil.
1433
1419
attr_reader :proxy_pass
1434
1420
end
@@ -1796,7 +1782,7 @@ def send_request(name, path, data = nil, header = nil)
1796
1782
#
1797
1783
# If +req+ is a Net::HTTP::Post or Net::HTTP::Put request containing
1798
1784
# data, the data is also sent. Providing data for a Net::HTTP::Head or
1799
- # Net::HTTP::Get request results in an ArgumentError.
1785
+ # \ Net::HTTP::Get request results in an ArgumentError.
1800
1786
#
1801
1787
# Returns an HTTPResponse object.
1802
1788
#
0 commit comments