make a http request with url parameters in ruby

To make an HTTP request with URL parameters in Ruby, you can use the Net::HTTP module. Here is an example of how to do it:

main.rb
require 'net/http'

uri = URI("http://example.com/search")
params = { :q => "ruby", :page => "2" }
uri.query = URI.encode_www_form(params)

res = Net::HTTP.get_response(uri)
puts res.body if res.is_a?(Net::HTTPSuccess)
219 chars
9 lines

In this example, we are making a request to http://example.com/search with two URL parameters: :q and :page. The URI module is used to encode the parameters and add them to the URI. Then Net::HTTP.get_response() is used to send the request and receive the response. Finally, we print the response body if the response was successful.

Note that this example uses a GET request, but you can also use POST or other HTTP methods with parameters. Additionally, you may need to add headers or authentication information depending on the API you are working with.

gistlibby LogSnag