Method: Gem::Net::HTTP#get

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

#get(path, initheader = nil, dest = nil, &block) ⇒ Object

:call-seq:

get(path, initheader = nil) {|res| ... }

Sends a GET request to the server; returns an instance of a subclass of Gem::Net::HTTPResponse.

The request is based on the Gem::Net::HTTP::Get object created from string path and initial headers hash initheader.

With a block given, calls the block with the response body:

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

Output:

"{\n  \"userId\": 1,\n  \"id\": 1,\n  \"title\": \"delectus aut autem\",\n  \"completed\": false\n}"

With no block given, simply returns the response object:

http.get('/') # => #<Gem::Net::HTTPOK 200 OK readbody=true>

Related:

  • Gem::Net::HTTP::Get: request class for HTTP method GET.

  • Gem::Net::HTTP.get: sends GET request, returns response body.



1987
1988
1989
1990
1991
1992
1993
1994
1995
# File 'lib/rubygems/vendor/net-http/lib/net/http.rb', line 1987

def get(path, initheader = nil, dest = nil, &block) # :yield: +body_segment+
  res = nil

  request(Get.new(path, initheader)) {|r|
    r.read_body dest, &block
    res = r
  }
  res
end