Skip to content
class method

Net::HTTPSession.get

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: