How to make a HTTP request using Ruby on Rails?

RubyRuby on-Rails-3HttpRequest

Ruby Problem Overview


I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.

How can I make this in Ruby on Rails?

If it is possible, is it a correct approach to use in my controllers?

Ruby Solutions


Solution 1 - Ruby

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

Solution 2 - Ruby

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:

Solution 3 - Ruby

OpenURI is the best; it's as simple as

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

Solution 4 - Ruby

require 'net/http'
result = Net::HTTP.get(URI.parse('http://www.example.com/about.html'))
# or
result = Net::HTTP.get(URI.parse('http://www.example.com'), '/about.html')

Solution 5 - Ruby

I prefer httpclient over Net::HTTP.

client = HTTPClient.new
puts client.get_content('http://www.example.com/index.html')

HTTParty is a good choice if you're making a class that's a client for a service. It's a convenient mixin that gives you 90% of what you need. See how short the Google and Twitter clients are in the examples.

And to answer your second question: no, I wouldn't put this functionality in a controller--I'd use a model instead if possible to encapsulate the particulars (perhaps using HTTParty) and simply call it from the controller.

Solution 6 - Ruby

My favorite two ways to grab the contents of URLs are either [OpenURI][1] or [Typhoeus][2].

OpenURI because it's everywhere, and Typhoeus because it's very flexible and powerful.

[1]: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open-uri/rdoc/OpenURI.html "open-uri" [2]:https://github.com/typhoeus/typhoeus

Solution 7 - Ruby

Here is the code that works if you are making a REST api call behind a proxy:

require "uri"
require 'net/http'
    
proxy_host = '<proxy addr>'
proxy_port = '<proxy_port>'
proxy_user = '<username>'
proxy_pass = '<password>'
    
uri = URI.parse("https://saucelabs.com:80/rest/v1/users/<username>")
proxy = Net::HTTP::Proxy(proxy_host, proxy_port, proxy_user, proxy_pass)
    
req = Net::HTTP::Get.new(uri.path)
req.basic_auth(<sauce_username>,<sauce_password>)
    
result = proxy.start(uri.host,uri.port) do |http|
http.request(req)
end
    
puts result.body

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser502052View Question on Stackoverflow
Solution 1 - RubyJoão SilvaView Answer on Stackoverflow
Solution 2 - RubystefView Answer on Stackoverflow
Solution 3 - Rubyuser2454031View Answer on Stackoverflow
Solution 4 - RubyAndrei AndrushkevichView Answer on Stackoverflow
Solution 5 - RubyMark ThomasView Answer on Stackoverflow
Solution 6 - Rubythe Tin ManView Answer on Stackoverflow
Solution 7 - RubymachzqcqView Answer on Stackoverflow