123456789_123456789_123456789_123456789_123456789_

Class: Gem::Net::HTTPRequest

Overview

This class is the base class for Gem::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 ::Gem::URI or a string hostname:

require 'rubygems/vendor/net-http/lib/net/http'
uri = Gem::URI('https://jsonplaceholder.typicode.com/')
req = Gem::Net::HTTP::Get.new(uri)          # => #<Gem::Net::HTTP::Get GET>
req = Gem::Net::HTTP::Get.new(uri.hostname) # => #<Gem::Net::HTTP::Get GET>

And with any of the subclasses:

req = Gem::Net::HTTP::Head.new(uri) # => #<Gem::Net::HTTP::Head HEAD>
req = Gem::Net::HTTP::Post.new(uri) # => #<Gem::Net::HTTP::Post POST>
req = Gem::Net::HTTP::Put.new(uri)  # => #<Gem::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:

You can add headers or override default headers:

#   res = Gem::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

MAX_FIELD_LENGTH, MAX_KEY_LENGTH

Class Method Summary

HTTPGenericRequest - Inherited

Instance Attribute Summary

HTTPGenericRequest - Inherited

#body

Returns the string body for the request, or nil if there is none:

#body=

Sets the body for the request:

#body_stream

Returns the body stream object for the request, or nil if there is none:

#body_stream=

Sets the body stream for the request:

#decode_content

Returns false if the request’s header 'Accept-Encoding' has been set manually or deleted (indicating that the user intends to handle encoding in the response), true otherwise:

#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 ::Gem::URI object for the request, or nil if none:

#body_exist?

HTTPHeader - Included

#chunked?

Returns true if field 'Transfer-Encoding' exists and has value 'chunked', false otherwise; see Transfer-Encoding response header:

#connection_close?

Returns whether the HTTP session is to be closed.

#connection_keep_alive?

Returns whether the HTTP session is to be kept alive.

#content_length

Returns the value of field 'Content-Length' as an integer, or nil if there is no such field; see Content-Length request header:

#content_length=

Sets the value of field 'Content-Length' to the given numeric; see Content-Length response header:

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 HTTP 1.1 and are expecting a 100-continue response.

#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 key, or nil if there is no such key; see Fields:

#[]=

Sets the value for the case-insensitive key to val, overwriting the previous value if the field exists; see Fields:

#add_field

Adds value val to the value array for field key if the field exists; creates the field with the given key and val if it does not exist.

#basic_auth

Sets header 'Authorization' using the given account and password strings:

#canonical_each
#content_range

Returns a Range object representing the value of field 'Content-Range', or nil if no such field exists; see Content-Range response header:

#content_type

Returns the media type from the value of field 'Content-Type', or nil if no such field exists; see Content-Type response header:

#content_type=
#delete

Removes the header for the given case-insensitive key (see Fields); returns the deleted value, or nil if no such field exists:

#each
#each_capitalized

Like #each_header, but the keys are returned in capitalized form.

#each_capitalized_name

Calls the block with each capitalized field name:

#each_header

Calls the block with each key/value pair:

#each_key
#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 key if it exists; otherwise returns the value of the block; ignores the default_val; see Fields:

#form_data=
#get_fields

Returns the array field value for the given key, or nil if there is no such field; see Fields:

#key?

Returns true if the field for the case-insensitive key exists, false otherwise:

#main_type

Returns the leading (‘type’) part of the media type from the value of field 'Content-Type', or nil if no such field exists; see Content-Type response header:

#proxy_basic_auth

Sets header 'Proxy-Authorization' using the given account and password strings:

#range

Returns an array of Range objects that represent the value of field 'Range', or nil if there is no such field; see Range request header:

#range=
#range_length

Returns the integer representing length of the value of field 'Content-Range', or nil if no such field exists; see Content-Range response header:

#set_content_type

Sets the value of field 'Content-Type'; returns the new value; see Content-Type request header:

#set_form

Stores form data to be used in a POST or PUT request.

#set_form_data

Sets the request body to a URL-encoded string derived from argument params, and sets request header field 'Content-Type' to 'application/x-www-form-urlencoded'.

#set_range

Sets the value for field 'Range'; see Range request header:

#sub_type

Returns the trailing (‘subtype’) part of the media type from the value of field 'Content-Type', or nil if no such field exists; see Content-Type response header:

#to_hash

Returns a hash of the key/value pairs:

#type_params

Returns the trailing (‘parameters’) part of the value of field 'Content-Type', or nil if no such field exists; see Content-Type response header:

#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

Creates an HTTP request object for Gem.path.

initheader are the default headers to use. HTTP adds Accept-Encoding to enable compression of the response body unless Accept-Encoding or Range are supplied in initheader.

[ GitHub ]

  
# File 'lib/rubygems/vendor/net-http/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