Using Net::Http.Get For an Https Url

Using Net::HTTP.get for an https url

Original answer:

uri = URI.parse("")
http =, 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("").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 = ""
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 =, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE

request =, request_header)
request.body = request_params.to_json


response = http.request(request)

You may find the following reference helpful:

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.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' ( It is fast, clean api.

Also you can take a look on the http clients comparison table:

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("");
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

var task = httpClient.PostAsXmlAsync<DeviceRequest>("api/SaveData", request);

Related Topics

Leave a reply