How do I touch a URL with rails?
-
28-10-2019 - |
Question
I have a json URL supplied by sendgrid. All it needs to be is touched. How would I do this?
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
Solution
You can use Net::HTTP.get
from standard library (see docs):
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}")
Updated:
For HTTPS you can do smth like that:
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)
Nice article on the subject - Ruby Net::HTTP Cheat Sheet.
OTHER TIPS
Install httpclient gem
HTTPClient.get("https://sendgrid.com/api/unsubscribes.add.xml?api_user=username%40website.com&api_key=secret_password&email=#{@user.email}")
maybe try ActionDispatch
GET http://api.rubyonrails.org/classes/ActionDispatch/Request.html#method-i-GET
require 'open-uri'
open("http://pragprog.com/") { |f| f.gets }
result? just one row, not the whole page:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\"\n"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow