🔍

No recent searches

# class Net::HTTPGenericRequest

HTTPGenericRequest is the parent of the `Net::HTTPRequest` class.

Do not use this directly; instead, use a subclass of `Net::HTTPRequest`.

## About the Examples

Examples here assume that `net/http` has been required (which also requires `uri`):

    require 'net/http'

Many code examples here use these example websites:

- jsonplaceholder.typicode.com.

- example.com.

Some examples also assume these variables:

    uri = URI('https://jsonplaceholder.typicode.com/')
    uri.freeze # Examples may not modify.
    hostname = uri.hostname # => "jsonplaceholder.typicode.com"
    path = uri.path         # => "/"
    port = uri.port         # => 443

So that example requests may be written as:

    Net::HTTP.get(uri)
    Net::HTTP.get(hostname, '/index.html')
    Net::HTTP.start(hostname) do |http|
      http.get('/todos/1')
      http.get('/todos/2')
    end

An example that needs a modified `URI` first duplicates `uri`, then modifies the duplicate:

    _uri = uri.dup
    _uri.path = '/todos/1'

### Attributes

body [R]

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

    req = Net::HTTP::Post.new(uri)
    req.body # => nil
    req.body = '{"title": "foo","body": "bar","userId": 1}'
    req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"

body_stream [R]

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

    req = Net::HTTP::Post.new(uri)          # => #<Net::HTTP::Post POST>
    req.body_stream                         # => nil
    require 'stringio'
    req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
    req.body_stream                         # => #<StringIO:0x0000027d1e5affa8>

decode_content [R]

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:

    req = Net::HTTP::Get.new(uri) # => #<Net::HTTP::Get GET>
    req['Accept-Encoding']        # => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
    req.decode_content            # => true
    req['Accept-Encoding'] = 'foo'
    req.decode_content            # => false
    req.delete('Accept-Encoding')
    req.decode_content            # => false

method [R]

Returns the string method name for the request:

    Net::HTTP::Get.new(uri).method  # => "GET"
    Net::HTTP::Post.new(uri).method # => "POST"

path [R]

Returns the string path for the request:

    Net::HTTP::Get.new(uri).path # => "/"
    Net::HTTP::Post.new('example.com').path # => "example.com"

uri [R]

Returns the `URI` object for the request, or `nil` if none:

    Net::HTTP::Get.new(uri).uri
    # => #<URI::HTTPS https://jsonplaceholder.typicode.com/>
    Net::HTTP::Get.new('example.com').uri # => nil

### Public Instance Methods

body= (str)
Source

    # File lib/net/http/generic_request.rb, line 176
    def body=(str)
      @body = str
      @body_stream = nil
      @body_data = nil
      str
    end

Sets the body for the request:

    req = Net::HTTP::Post.new(uri)
    req.body # => nil
    req.body = '{"title": "foo","body": "bar","userId": 1}'
    req.body # => "{\"title\": \"foo\",\"body\": \"bar\",\"userId\": 1}"

body_stream= (input)
Source

    # File lib/net/http/generic_request.rb, line 201
    def body_stream=(input)
      @body = nil
      @body_stream = input
      @body_data = nil
      input
    end

Sets the body stream for the request:

    req = Net::HTTP::Post.new(uri)          # => #<Net::HTTP::Post POST>
    req.body_stream                         # => nil
    require 'stringio'
    req.body_stream = StringIO.new('xyzzy') # => #<StringIO:0x0000027d1e5affa8>
    req.body_stream                         # => #<StringIO:0x0000027d1e5affa8>

inspect ()
Source

    # File lib/net/http/generic_request.rb, line 98
    def inspect
      "\#<#{self.class} #{@method}>"
    end

Returns a string representation of the request:

    Net::HTTP::Post.new(uri).inspect # => "#<Net::HTTP::Post POST>"

pretty_print (q)
Source

    # File lib/net/http/generic_request.rb, line 116
    def pretty_print(q)
      q.object_group(self) {
        q.breakable
        q.text @method
        q.breakable
        q.text "path="; q.pp @path
        q.breakable
        q.text "headers="; q.pp to_hash
      }
    end

Returns a string representation of the request with the details for pp:

    require 'pp'
    post = Net::HTTP::Post.new(uri)
    post.inspect # => "#<Net::HTTP::Post POST>"
    post.pretty_inspect
    # => #<Net::HTTP::Post
          POST
          path="/"
          headers={"accept-encoding" => ["gzip;q=1.0,deflate;q=0.6,identity;q=0.3"],
           "accept" => ["*/*"],
           "user-agent" => ["Ruby"],
           "host" => ["www.ruby-lang.org"]}>

request_body_permitted? ()
Source

    # File lib/net/http/generic_request.rb, line 142
    def request_body_permitted?
      @request_has_body
    end

Returns whether the request may have a body:

    Net::HTTP::Post.new(uri).request_body_permitted? # => true
    Net::HTTP::Get.new(uri).request_body_permitted?  # => false

response_body_permitted? ()
Source

    # File lib/net/http/generic_request.rb, line 151
    def response_body_permitted?
      @response_has_body
    end

Returns whether the response may have a body:

    Net::HTTP::Post.new(uri).response_body_permitted? # => true
    Net::HTTP::Head.new(uri).response_body_permitted? # => false