Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

Net::HTTP.get

(::URI::Generic uri, ?::Net::headers header) -> ::String
(::String host, ::String path, ?::Integer port) -> ::String
Net::HTTP.get(hostname, path, port = 80) → body
Net::HTTP:get(uri, headers = {}, port = uri.port) → body

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:

See also #get .default_port .get_print .get_response also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0