Ruby on rails 如何使用rails访问URL?
我有一个由sendgrid提供的json URL。它需要的只是触摸。我该怎么做Ruby on rails 如何使用rails访问URL?,ruby-on-rails,url,touch,sendgrid,Ruby On Rails,Url,Touch,Sendgrid,我有一个由sendgrid提供的json URL。它需要的只是触摸。我该怎么做 def suspend @user = User.find(params[:id]) @user.update_attribute("suspended", true) # the url I need to touch => https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_
def suspend
@user = User.find(params[:id])
@user.update_attribute("suspended", true)
# the url I need to touch => https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_key=secret_password&email=#{@user.email}
end
也许可以尝试
ActionDispatch
GET安装httpclient-gem
HTTPClient.get("https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_key=secret_password&email=#{@user.email}")
您可以使用标准库中的
Net::HTTP.get
(请参阅):
更新:
对于HTTPS,您可以这样做:
require "net/https"
uri = URI.parse("https://www.google.com")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
# request = Net::HTTP::Head.new(uri.request_uri) - get response without body
response = http.request(request)
这篇文章写得不错
结果如何?只有一行,而不是整个页面:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
“您的答案是针对HTTP请求的,但它是发送到HTTPS的。因此我认为它需要一些不同的东西。太棒了!非常感谢您的独立想法!我完全是普通的,或者什么都没有。;)您也可以将其更改为头请求。谢谢,更新了答案。这似乎是HTTP头请求的一个很好的用例。
require 'open-uri'
open("http://pragprog.com/") { |f| f.gets }
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"