1. Net::
  2. HTTPRequest

class Net::HTTPRequest

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 aURI 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 toNet::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 = Net::HTTP::Get.new(uri, {'foo' => '0', 'bar' => '1'})

This class (and therefore its subclasses) also includes (indirectly) moduleNet::HTTPHeader, which gives access to itsmethods for setting headers.

Request Subclasses

Subclasses for HTTP requests:

Subclasses for WebDAV requests:

Public Class Methods

Source
# File lib/net/http/request.rb, line 82definitialize(path,initheader =nil)superself.class::METHOD,self.class::REQUEST_HAS_BODY,self.class::RESPONSE_HAS_BODY,path,initheaderend

Creates an HTTP request object forpath.

initheader are the default headers to use.Net::HTTP adds Accept-Encoding to enable compression of the response body unless Accept-Encoding orRange are supplied ininitheader.

Calls superclass methodBasicObject::new