1
1
# frozen_string_literal: false
2
- # HTTPGenericRequest is the parent of the Net::HTTPRequest class.
3
- # Do not use this directly; use a subclass of Net::HTTPRequest.
4
2
#
5
- # Mixes in the Net::HTTPHeader module to provide easier access to HTTP headers.
3
+ # \HTTPGenericRequest is the parent of the Net::HTTPRequest class.
4
+ #
5
+ # Do not use this directly; instead, use a subclass of Net::HTTPRequest.
6
+ #
7
+ # == About the Examples
8
+ #
9
+ # :include: doc/net-http/examples.rdoc
6
10
#
7
11
class Net ::HTTPGenericRequest
8
12
9
13
include Net ::HTTPHeader
10
14
11
- def initialize ( m , reqbody , resbody , uri_or_path , initheader = nil )
15
+ def initialize ( m , reqbody , resbody , uri_or_path , initheader = nil ) # :nodoc:
12
16
@method = m
13
17
@request_has_body = reqbody
14
18
@response_has_body = resbody
@@ -53,15 +57,47 @@ def initialize(m, reqbody, resbody, uri_or_path, initheader = nil)
53
57
@body_data = nil
54
58
end
55
59
60
+ # Returns the string method name for the request:
61
+ #
62
+ # Net::HTTP::Get.new(uri).method # => "GET"
63
+ # Net::HTTP::Post.new(uri).method # => "POST"
64
+ #
56
65
attr_reader :method
66
+
67
+ # Returns the string path for the request:
68
+ #
69
+ # Net::HTTP::Get.new(uri).path # => "/"
70
+ # Net::HTTP::Post.new('example.com').path # => "example.com"
71
+ #
57
72
attr_reader :path
73
+
74
+ # Returns the URI object for the request, or +nil+ if none:
75
+ #
76
+ # Net::HTTP::Get.new(uri).uri
77
+ # # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
78
+ # Net::HTTP::Get.new('example.com').uri # => nil
79
+ #
58
80
attr_reader :uri
59
81
60
- # Automatically set to false if the user sets the Accept-Encoding header.
61
- # This indicates they wish to handle Content-encoding in responses
62
- # themselves.
82
+ # Returns +false+ if the request's header <tt>'Accept-Encoding'</tt>
83
+ # has been set manually or deleted
84
+ # (indicating that the user intends to handle encoding in the response),
85
+ # +true+ otherwise:
86
+ #
87
+ # req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
88
+ # req['Accept-Encoding'] # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
89
+ # req.decode_content # => true
90
+ # req['Accept-Encoding'] = 'foo'
91
+ # req.decode_content # => false
92
+ # req.delete('Accept-Encoding')
93
+ # req.decode_content # => false
94
+ #
63
95
attr_reader :decode_content
64
96
97
+ # Returns a string representation of the request:
98
+ #
99
+ # Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"
100
+ #
65
101
def inspect
66
102
"\# <#{ self . class } #{ @method } >"
67
103
end
@@ -76,30 +112,70 @@ def []=(key, val) # :nodoc:
76
112
super key , val
77
113
end
78
114
115
+ # Returns whether the request may have a body:
116
+ #
117
+ # Net::HTTP::Post.new(uri).request_body_permitted? # => true
118
+ # Net::HTTP::Get.new(uri).request_body_permitted? # => false
119
+ #
79
120
def request_body_permitted?
80
121
@request_has_body
81
122
end
82
123
124
+ # Returns whether the response may have a body:
125
+ #
126
+ # Net::HTTP::Post.new(uri).response_body_permitted? # => true
127
+ # Net::HTTP::Head.new(uri).response_body_permitted? # => false
128
+ #
83
129
def response_body_permitted?
84
130
@response_has_body
85
131
end
86
132
87
- def body_exist?
133
+ def body_exist? # :nodoc:
88
134
warn "Net::HTTPRequest#body_exist? is obsolete; use response_body_permitted?" , uplevel : 1 if $VERBOSE
89
135
response_body_permitted?
90
136
end
91
137
138
+ # Returns the string body for the request, or +nil+ if there is none:
139
+ #
140
+ # req = Net::HTTP::Post.new(uri)
141
+ # req.body # => nil
142
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
143
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
144
+ #
92
145
attr_reader :body
93
146
147
+ # Sets the body for the request:
148
+ #
149
+ # req = Net::HTTP::Post.new(uri)
150
+ # req.body # => nil
151
+ # req.body = '{"title": "foo","body": "bar","userId": 1}'
152
+ # req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"
153
+ #
94
154
def body = ( str )
95
155
@body = str
96
156
@body_stream = nil
97
157
@body_data = nil
98
158
str
99
159
end
100
160
161
+ # Returns the body stream object for the request, or +nil+ if there is none:
162
+ #
163
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
164
+ # req.body_stream # => nil
165
+ # require 'stringio'
166
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
167
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
168
+ #
101
169
attr_reader :body_stream
102
170
171
+ # Sets the body stream for the request:
172
+ #
173
+ # req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
174
+ # req.body_stream # => nil
175
+ # require 'stringio'
176
+ # req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
177
+ # req.body_stream # => #<StringIO:0x0000027d1e5affa8>
178
+ #
103
179
def body_stream = ( input )
104
180
@body = nil
105
181
@body_stream = input
0 commit comments