Skip to content
instance method

Net::HTTP#get

(::String path, ?::Net::headers initheader, ?bot dest) ?{ (::String body_segment) -> void } -> ::Net::HTTPResponse
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:

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

Output:

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

With no block given, simply returns the response object:

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

Related:

See also .get #finish #head #delete also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0