create a get request handler in ruby

To create a GET request handler in Ruby, you can use any web framework such as Sinatra, Ruby on Rails, or simply Net::HTTP.

Here is an example of creating a GET request handler using Sinatra:

main.rb
require 'sinatra'

get '/hello' do
  "Hello World!"
end
56 chars
6 lines

This code creates an HTTP GET request handler for the /hello endpoint. When the endpoint is requested, the server responds with "Hello World!".

Here is an example of creating a GET request handler using Net::HTTP:

main.rb
require 'net/http'

uri = URI('http://example.com')

response = Net::HTTP.get(uri)

p response
95 chars
8 lines

This code creates an HTTP GET request to the example.com domain and returns the response body.

related categories

gistlibby LogSnag