Method: Gem::Net::HTTP#request

Defined in:
lib/rubygems/vendor/net-http/lib/net/http.rb

#request(req, body = nil, &block) ⇒ Object

Sends the given request req to the server; forms the response into a Gem::Net::HTTPResponse object.

The given req must be an instance of a subclass of Gem::Net::HTTPRequest. Argument body should be given only if needed for the request.

With no block given, returns the response object:

http = Gem::Net::HTTP.new(hostname)

req = Gem::Net::HTTP::Get.new('/todos/1')
http.request(req)
# => #<Gem::Net::HTTPOK 200 OK readbody=true>

req = Gem::Net::HTTP::Post.new('/todos')
http.request(req, 'xyzzy')
# => #<Gem::Net::HTTPCreated 201 Created readbody=true>

With a block given, calls the block with the response and returns the response:

req = Gem::Net::HTTP::Get.new('/todos/1')
http.request(req) do |res|
  p res
end # => #<Gem::Net::HTTPOK 200 OK readbody=true>

Output:

#<Gem::Net::HTTPOK 200 OK readbody=false>


2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
# File 'lib/rubygems/vendor/net-http/lib/net/http.rb', line 2373

def request(req, body = nil, &block)  # :yield: +response+
  unless started?
    start {
      req['connection'] ||= 'close'
      return request(req, body, &block)
    }
  end
  if proxy_user()
    req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl?
  end
  req.set_body_internal body
  res = transport_request(req, &block)
  if sspi_auth?(res)
    sspi_auth(req)
    res = transport_request(req, &block)
  end
  res
end