Using Net::HTTP.get for an https url
Original answer:
uri = URI.parse("https://example.com/some/path")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
@data = http.get(uri.request_uri)
As pointed out in the comments, this is more elegant:
require "open-uri"
@data = URI.parse("https://example.com/some/path").read
Ruby net/http/get does not work with url's, but works with uri's. Why?
The difference is that your url
is an instance of String
which happens to be a URL. But because it is just a simple string is has no special meaning.
Whereas uri
is an instance of URI
which is not only a simple string but had the URL from the string already analyzed and it now offers several optimized methods to return specific parts of the URL – like the protocol, the hostname, the path or query parameters.
POST request to HTTPS using Net::HTTP
Your request
hash is being replaced by your request
object which you're assigning Net::HTTP
. Also be sure to set request params in the body of your HTTP request:
require "active_support/all"
require "net/http"
token = "my_token"
url = "https://graph.facebook.com/v2.6/me/messages?"
sender = 100688998246663
text = "Hello"
request_params = {
recipient: {id: sender},
message: {text: text},
access_token: token
}
request_header = { 'Content-Type': 'application/json' }
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri.path, request_header)
request.body = request_params.to_json
http.request(request)
response = http.request(request)
You may find the following reference helpful: http://www.rubyinside.com/nethttp-cheat-sheet-2940.html
HTTPS request using Net::HTTP's block form -- is it possible?
See the documentation for Net::HTTP.start which takes an optional hash. From the documentation:
opt sets following values by its accessor. The keys are ca_file, ca_path, cert, cert_store, ciphers, close_on_empty_response, key, open_timeout, read_timeout, ssl_timeout, ssl_version, use_ssl, verify_callback, verify_depth and verify_mode. If you set :use_ssl as true, you can use https and default value of verify_mode is set as OpenSSL::SSL::VERIFY_PEER.
Net::HTTP.start(url.host, url.port, :use_ssl => true)
How to get URL in http.Request
Fastest Net::HTTP/Net::HTTPS wrapper for Ruby
i prefer to use gem 'http'
(https://github.com/httprb/http). It is fast, clean api.
Also you can take a look on the http clients comparison table:
https://github.com/httprb/http#another-ruby-http-library-why-should-i-care
Make Https call using HttpClient
If the server only supports higher TLS version like TLS 1.2 only, it will still fail unless your client PC is configured to use higher TLS version by default. To overcome this problem, add the following in your code:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Modifying your code example, it would be
HttpClient httpClient = new HttpClient();
//specify to use TLS 1.2 as default connection
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
httpClient.BaseAddress = new Uri("https://foobar.com/");
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var task = httpClient.PostAsXmlAsync<DeviceRequest>("api/SaveData", request);
Related Topics
Regex for Checking the Last Character
Best Way to Escape and Unescape Strings in Ruby
Reducing N+1 Queries Using the Bullet and Rspec Gems
What Does @@Variable Mean in Ruby
Rails Keeps Telling Me That It's Not Currently Installed
Rhc Setup Gives Error 'No Such File Dl/Import'
How to Specify a Local Gem in My Gemfile
Pass Variables to Ruby Script Via Command Line
Why Does Ruby Have Both Private and Protected Methods
Using Rails Serialize to Save Hash to Database
How to Determine If One Array Contains All Elements of Another Array
Test If String Is a Number in Ruby on Rails
Require': Cannot Load Such File - 'Nokogiri\Nokogiri' (Loaderror) When Running 'Rails Server'