How to make a HTTP request using Ruby on Rails? How to make a HTTP request using Ruby on Rails? ruby ruby

How to make a HTTP request using Ruby on Rails?


You can use Ruby's Net::HTTP class:

require 'net/http'url = URI.parse('http://www.example.com/index.html')req = Net::HTTP::Get.new(url.to_s)res = Net::HTTP.start(url.host, url.port) {|http|  http.request(req)}puts res.body


Net::HTTP is built into Ruby, but let's face it, often it's easier not to use its cumbersome 1980s style and try a higher level alternative:


OpenURI is the best; it's as simple as

require 'open-uri'response = open('http://example.com').read