Mridang Agarwalla
Mridang Agarwalla

Reputation: 44958

Fetch URL (with params) using Ruby

Could someone tell me how I can fetch (GET) a URL (with params) using Ruby? I found a bunch of examples online but I couldn't find one that explained how I can also pass the parameters.

Upvotes: 3

Views: 8481

Answers (3)

Mridang Agarwalla
Mridang Agarwalla

Reputation: 44958

I missed this one. The solutions are here.

Parametrized get request in Ruby?

Upvotes: 2

Simone Carletti
Simone Carletti

Reputation: 176352

require 'net/http'
require 'uri'

uri = URI.parse("http://www.example.com/?test=1")
response = Net::HTTP.get_response uri
p response.body

There are also some other good HTTP clients or wrappers, such as HTTParty.

require 'rubygems'
require 'httparty'

response = HTTParty.get("http://www.example.com/?test=1")
p response.body

Upvotes: 6

Gareth
Gareth

Reputation: 138002

I use something like the following, it's pretty simple and doesn't make you build your own query string:

require 'net/http'
response = nil
Net::HTTP.start "example.com", 80 do |http|
  request = Net::HTTP::Get.new "/endpoint"
  request.form_data = {:q => "123"}
  response = http.request(request)
end

Upvotes: 2

Related Questions