1

I have a Ruby code:

require 'net/http'
url = "https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time"
response = Net::HTTP.get_response(url, '/')

Which produces errors:

getaddrinfo: nodename nor servname provided, or not known (SocketError)

and

Failed to open TCP connection to https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time:80 (getaddrinfo: nodename nor servname provided, or not known) (SocketError)

But it works perfectly with uri:

require 'net/http'
url = "https://stackoverflow.com/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time"
uri = URI(url)
response = Net::HTTP.get_response(uri)

So, could anyone explain what is the difference, why it works so, and what is so special about URI?

Laughing_Man
  • 179
  • 16
  • 3
    [Documentation](https://docs.ruby-lang.org/en/2.0.0/Net/HTTP.html#method-c-get_response) mentions that `The target can either be specified as (uri), or as (host, path, port = 80)` So for first call to succeed it should have been `Net::HTTP.get_response('stackoverflow.com', '/questions/65798852/how-do-i-set-an-electron-variable-at-compile-time')` – rubish Jan 29 '21 at 10:27

1 Answers1

0

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.

spickermann
  • 100,941
  • 9
  • 101
  • 131