Class: Net::HTTPRequest
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
HTTPGenericRequest
|
|
Instance Chain:
self,
HTTPGenericRequest ,
HTTPHeader
|
|
Inherits: |
Net::HTTPGenericRequest
|
Defined in: | lib/net/http/request.rb |
Overview
This class is the base class for Net::HTTP request classes. The class should not be used directly; instead you should use its subclasses, listed below.
Creating a Request
An request object may be created with either a URI or a string hostname:
require 'net/http'
uri = URI('https://jsonplaceholder.typicode.com/')
req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
req = Net::HTTP::Get.new(uri.hostname) # => #<Net::HTTP::Get GET>
And with any of the subclasses:
req = Net::HTTP::Head.new(uri) # => #<Net::HTTP::Head HEAD>
req = Net::HTTP::Post.new(uri) # => #<Net::HTTP::Post POST>
req = Net::HTTP::Put.new(uri) # => #<Net::HTTP::Put PUT>
# ...
The new instance is suitable for use as the argument to HTTP#request.
Request Headers
A new request object has these header fields by default:
req.to_hash
# =>
{"accept-encoding"=>["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
"accept"=>["*/*"],
"user-agent"=>["Ruby"],
"host"=>["jsonplaceholder.typicode.com"]}
See:
-
Request header Accept-Encoding and
Compression and Decompression
.
You can add headers or override default headers:
# res = Net::HTTP::Get.new(uri, {'foo' => '0', 'bar' => '1'})
This class (and therefore its subclasses) also includes (indirectly) module HTTPHeader
, which gives access to its methods for setting headers
.
Request Subclasses
Subclasses for HTTP
requests:
Subclasses for WebDAV requests:
Constant Summary
HTTPHeader
- Included
Class Method Summary
-
.new(path, initheader = nil) ⇒ HTTPRequest
constructor
Creates an
HTTP
request object forpath
.
HTTPGenericRequest
- Inherited
Instance Attribute Summary
HTTPGenericRequest
- Inherited
#body | Returns the string body for the request, or |
#body= | Sets the body for the request: |
#body_stream | Returns the body stream object for the request, or |
#body_stream= | Sets the body stream for the request: |
#decode_content | Returns |
#method | Returns the string method name for the request: |
#path | Returns the string path for the request: |
#request_body_permitted? | Returns whether the request may have a body: |
#response_body_permitted? | Returns whether the response may have a body: |
#uri | Returns the URI object for the request, or |
#body_exist? |
HTTPHeader
- Included
#chunked? | Returns |
#connection_close? | Returns whether the |
#connection_keep_alive? | Returns whether the |
#content_length | Returns the value of field |
#content_length= | Sets the value of field |
Instance Method Summary
HTTPGenericRequest
- Inherited
#encode_multipart_form_data, #flush_buffer, | |
#inspect | Returns a string representation of the request: |
#quote_string, #send_request_with_body, #send_request_with_body_data, #send_request_with_body_stream, #supply_default_content_type, | |
#wait_for_continue | Waits up to the continue timeout for a response from the server provided we’re speaking |
#write_header, | |
#[]= | Don’t automatically decode response content-encoding if the user indicates they want to handle it. |
#exec | write. |
#set_body_internal | internal use only. |
#update_uri | internal use only. |
HTTPHeader
- Included
#[] | Returns the string field value for the case-insensitive field |
#[]= | Sets the value for the case-insensitive |
#add_field | Adds value |
#basic_auth | Sets header |
#canonical_each | Alias for HTTPHeader#each_capitalized. |
#content_range | Returns a Range object representing the value of field |
#content_type | Returns the media type from the value of field |
#content_type= | Alias for HTTPHeader#set_content_type. |
#delete | Removes the header for the given case-insensitive |
#each | Alias for HTTPHeader#each_header. |
#each_capitalized | Like |
#each_capitalized_name | Calls the block with each capitalized field name: |
#each_header | Calls the block with each key/value pair: |
#each_key | Alias for HTTPHeader#each_name. |
#each_name | Calls the block with each field key: |
#each_value | Calls the block with each string field value: |
#fetch | With a block, returns the string value for |
#form_data= | Alias for HTTPHeader#set_form_data. |
#get_fields | Returns the array field value for the given |
#key? | Returns |
#main_type | Returns the leading (‘type’) part of the media type from the value of field |
#proxy_basic_auth | Sets header |
#range | Returns an array of Range objects that represent the value of field |
#range= | Alias for HTTPHeader#set_range. |
#range_length | Returns the integer representing length of the value of field |
#set_content_type | Sets the value of field |
#set_form | Stores form data to be used in a |
#set_form_data | Sets the request body to a URL-encoded string derived from argument |
#set_range | Sets the value for field |
#sub_type | Returns the trailing (‘subtype’) part of the media type from the value of field |
#to_hash | Returns a hash of the key/value pairs: |
#type_params | Returns the trailing (‘parameters’) part of the value of field |
#append_field_value, #basic_encode, #capitalize, #set_field, #initialize_http_header, | |
#length | Alias for HTTPHeader#size. |
#size | obsolete. |
Constructor Details
.new(path, initheader = nil) ⇒ HTTPRequest
# File 'lib/net/http/request.rb', line 82
def initialize(path, initheader = nil) super self.class::METHOD, self.class::REQUEST_HAS_BODY, self.class::RESPONSE_HAS_BODY, path, initheader end