Class: Gem::Net::HTTPRequest

Inherits:
HTTPGenericRequest show all
Defined in:
lib/rubygems/vendor/net-http/lib/net/http/request.rb

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 Gem::Net::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 Gem::Net::HTTPHeader, which gives access to its methods for setting headers.

Request Subclasses

Subclasses for HTTP requests:

  • Gem::Net::HTTP::Get

  • Gem::Net::HTTP::Head

  • Gem::Net::HTTP::Post

  • Gem::Net::HTTP::Put

  • Gem::Net::HTTP::Delete

  • Gem::Net::HTTP::Options

  • Gem::Net::HTTP::Trace

  • Gem::Net::HTTP::Patch

Subclasses for WebDAV requests:

  • Gem::Net::HTTP::Propfind

  • Gem::Net::HTTP::Proppatch

  • Gem::Net::HTTP::Mkcol

  • Gem::Net::HTTP::Copy

  • Gem::Net::HTTP::Move

  • Gem::Net::HTTP::Lock

  • Gem::Net::HTTP::Unlock

Constant Summary

Constants included from HTTPHeader

Gem::Net::HTTPHeader::MAX_FIELD_LENGTH, Gem::Net::HTTPHeader::MAX_KEY_LENGTH

Instance Attribute Summary

Attributes inherited from HTTPGenericRequest

#body, #body_stream, #decode_content, #method, #path, #uri

Instance Method Summary collapse

Methods inherited from HTTPGenericRequest

#[]=, #body_exist?, #exec, #inspect, #request_body_permitted?, #response_body_permitted?, #set_body_internal, #update_uri

Methods included from HTTPHeader

#[], #[]=, #add_field, #basic_auth, #chunked?, #connection_close?, #connection_keep_alive?, #content_length, #content_length=, #content_range, #content_type, #delete, #each_capitalized, #each_capitalized_name, #each_header, #each_name, #each_value, #fetch, #get_fields, #initialize_http_header, #key?, #main_type, #proxy_basic_auth, #range, #range_length, #set_content_type, #set_form, #set_form_data, #set_range, #size, #sub_type, #to_hash, #type_params

Constructor Details

#initialize(path, initheader = nil) ⇒ HTTPRequest

Creates an HTTP request object for path.

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



82
83
84
85
86
87
# 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