0

我有一个 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_key=secret_password&email=#{@user.email}

end
4

4 回答 4

3

您可以Net::HTTP.get从标准库中使用(请参阅文档):

require 'net/http'
Net::HTTP.get URI("https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_key=secret_password&email=#{@user.email}")

更新:

对于 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)

关于这个主题的好文章 - Ruby Net::HTTP Cheat Sheet

于 2011-12-05T15:32:58.983 回答
2

安装 httpclient gem

HTTPClient.get("https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_key=secret_password&email=#{@user.email}")
于 2011-12-05T15:28:30.470 回答
1

也许尝试ActionDispatchGET http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-GET

于 2011-12-05T15:28:27.103 回答
1
require 'open-uri'
open("http://pragprog.com/") { |f| f.gets  }

结果?只有一行,而不是整个页面:

"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
于 2011-12-05T15:48:22.340 回答