1

Ruby v2 introduced Net:HTTP.get(uri) which allowed for handling HTTPS URI objects seamlessly.

For example, you can call Net:HTTP.get(URI('https://google.com')) without any special incantations.

However, the minute I supply any headers as a second argument. I get a type error.

require 'net/http'
Net::HTTP.get(URI('https://google.com'), { 'Accept': 'text/html' }) 
# => Caused by TypeError: no implicit conversion of URI::HTTPS into String

Is there any workaround that doesn't require using the older methods?

fny
  • 31,255
  • 16
  • 96
  • 127
  • 1
    Example from docs: `Net::HTTP.get(URI('http://www.example.com/index.html'), { 'Accept' => 'text/html' })` - notice that headers are separate argument from uri – Fabio Oct 31 '21 at 00:17
  • You didn't read my question carefully. I give an example using that same syntax as an example, and it doesn't work for HTTPS. – fny Oct 31 '21 at 04:52
  • 1
    In your example you are passing only one argument to the `get` method: `URI('https://google.com', { 'Accept': 'text/html' })` – Fabio Oct 31 '21 at 05:30
  • Does this answer your question? [Using Net::HTTP.get for an https url](https://stackoverflow.com/questions/5786779/using-nethttp-get-for-an-https-url) – Fabio Oct 31 '21 at 05:34
  • 1
    That's a typo. Updated. Just copy and paste `require 'net/http'; Net::HTTP.get(URI('https://google.com'), { 'Accept': 'text/html' })` into IRB, you'll get the error. – fny Oct 31 '21 at 20:49
  • _which allowed for handling HTTPS URI objects seamlessly_ - can you point to the documentation which says that? – Fabio Oct 31 '21 at 23:52
  • See the source code: https://github.com/ruby/net-http/blob/f3e65e2a313203ff53302dd2feb1cda712e2a24b/lib/net/http.rb#L485 – fny Nov 03 '21 at 02:10

1 Answers1

4

I just had this same issue. My problem was that my ruby version (2.7) does not support passing headers to Net::HTTP.get.

https://ruby-doc.org/stdlib-2.7.0/libdoc/net/http/rdoc/Net/HTTP.html#get-method

The headers support has been included in ruby 3

https://ruby-doc.org/stdlib-3.0.0/libdoc/net/http/rdoc/Net/HTTP.html#get-method

Laura Paakkinen
  • 1,661
  • 14
  • 22