如何使用 Ruby on Rails 发出 HTTP 请求?

发布于 2024-10-10 05:19:06 字数 136 浏览 2 评论 0原文

我想从另一个网站获取信息。因此(也许)我应该向该网站发出请求(在我的例子中是 HTTP GET 请求)并接收响应。

我怎样才能在 Ruby on Rails 中做到这一点?

如果可能的话,在我的控制器中使用它是否是正确的方法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

瀟灑尐姊 2024-10-17 05:19:06

您可以使用 Ruby 的 Net::HTTP< /code>类:

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

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
紫罗兰の梦幻 2024-10-17 05:19:06

Net::HTTP 内置于 Ruby 中,但让我们面对现实吧,通常使用其繁琐的 1980 年代风格并尝试更高级别的替代方案会更容易:

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:

余生再见 2024-10-17 05:19:06

OpenURI 是最好的;就这么简单

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

OpenURI is the best; it's as simple as

require 'open-uri'
response = open('http://example.com').read
折戟 2024-10-17 05:19:06
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')
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')
我做我的改变 2024-10-17 05:19:06

我更喜欢 httpclient 而不是 Net::HTTP。

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

如果您要创建一个作为服务客户端的类,那么 HTTParty 是一个不错的选择。这是一个方便的 mixin,可以满足您 90% 的需求。在示例中了解 Google 和 Twitter 客户端有多短。

回答你的第二个问题:不,我不会把这个功能放在控制器中——如果可能的话,我会使用模型来封装细节(也许使用 HTTParty),然后简单地从控制器调用它。

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.

浮萍、无处依 2024-10-17 05:19:06

如果您在代理后面进行 REST api 调用,则以下代码有效:

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

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
心的憧憬 2024-10-17 05:19:06

我最喜欢的两种获取 URL 内容的方法是 OpenURITyphoeus

OpenURI 因为它无处不在,而 Typhoeus 因为它非常灵活和强大。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文