make a get request in ruby

You can use Ruby's built-in Net::HTTP library to make HTTP requests, including GET requests. Here's an example:

main.rb
require 'net/http'

url = URI.parse('https://www.example.com/api/data')
request = Net::HTTP::Get.new(url)

response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) do |http|
  http.request(request)
end

puts response.body
231 chars
11 lines

In this example, we first parse the URL we want to make a request to using URI.parse(). We then create a new Net::HTTP::Get object, passing in the parsed URI. We make the request using Net::HTTP.start(), passing in the hostname, port, and use_ssl flag. Finally, we output the response's body to the console using puts response.body.

related categories

gistlibby LogSnag