Method: Gem::Net::HTTP.get

Defined in:
lib/rubygems/vendor/net-http/lib/net/http.rb

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

:call-seq:

Gem::Net::HTTP.get(hostname, path, port = 80) -> body
Gem::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 Gem::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 = Gem::URI('https://jsonplaceholder.typicode.com/todos/1')
headers = {'Content-type' => 'application/json; charset=UTF-8'}
Gem::Net::HTTP.get(uri, headers)

Related:

  • Gem::Net::HTTP::Get: request class for HTTP method GET.

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



810
811
812
# File 'lib/rubygems/vendor/net-http/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