Method: Net::HTTP.get

Defined in:
lib/net/http.rb

.get(uri_or_host, path_or_headers = nil, port = nil) ⇒ Object

:call-seq:

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:

hostname = 'jsonplaceholder.typicode.com'
path = '/todos/1'
puts Net::HTTP.get(hostname, path)

Output:

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

With URI object uri and optional hash argument headers:

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 method GET.

  • Net::HTTP#get: convenience method for HTTP method GET.



810
811
812
# File 'lib/net/http.rb', line 810

def HTTP.get(uri_or_host, path_or_headers = nil, port = nil)
  get_response(uri_or_host, path_or_headers, port).body
end