instance method
Net::HTTPSession#get
get(path, initheader = nil) {|res| ... }
lib/net/http.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/net/http.rb, line 2013
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
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:
-
Net::HTTP::Get: request class for HTTP method GET. -
Net::HTTP.get: sends GET request, returns response body.