Method: Net::HTTP#get

Defined in:
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 Net::HTTPResponse.

The request is based on the 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 = Net::HTTP.new(hostname)
http.get('/todos/1') do |res|
  p res
end # => #<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('/') # => #<Net::HTTPOK 200 OK readbody=true>

Related:

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

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



1914
1915
1916
1917
1918
1919
1920
1921
1922
# File 'lib/net/http.rb', line 1914

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