Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
Net::HTTP.get
Net::HTTP.get(hostname, path, port = 80) → body
Net::HTTP:get(uri, headers = {}, port = uri.port) → body
lib/net/http.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/lib/net/http.rb, line 802
def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
get_response(uri_or_host, path_or_headers, port).body
end
Sends a GET request and returns the HTTP response body as a string.
With string arguments hostname and path:
Example
hostname = 'jsonplaceholder.typicode.com' path = '/todos/1' puts Net::HTTP.get(hostname, path)
Output:
Example
{
"userId": 1,
"id": 1,
"title": "delectus aut autem",
"completed": false
}
With URI object uri and optional hash argument headers:
Example
uri = URI('https://jsonplaceholder.typicode.com/todos/1') headers = {'Content-type' => 'application/json; charset=UTF-8'} Net::HTTP.get(uri, headers)
Related:
-
Net::HTTP::Get: request class for HTTP methodGET. -
Net::HTTP#get: convenience method for HTTP methodGET.