malt3
malt3

Reputation: 91

Ruby Net::HTTP Get request Error when using https

I have a web server that i need to get some data from. I want to reuse the connection, that's why i chose HTTP.new

require "net/http"
require "openssl"
uri = URI.parse("https://example.com")
path = "/index.html"
http = Net::HTTP.new(uri.host, uri.port,
    :use_ssl => uri.scheme == 'https')
request = Net::HTTP::Get.new "/index.html"
response = http.request(request)

When issuing the request (response = http.request(request)) ruby throws a TypeError:

TypeError: no implicit conversion of Hash into String
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `initialize'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `open'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:879:in `block in connect'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/timeout.rb:74:in `timeout'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:878:in `connect'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:863:in `do_start'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:852:in `start'
    from /usr/local/Cellar/ruby/2.2.2/lib/ruby/2.2.0/net/http.rb:1375:in `request'
    from (irb):14
    from /usr/local/bin/irb:11:in `<main>'

The same request works when i use http

Upvotes: 2

Views: 2992

Answers (1)

Sandy Cash
Sandy Cash

Reputation: 406

I'm not sure you aren't overcomplicating the invocation. Why not just use:

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true

I'm basing this on the assumption that you already know whether or not your target server requires HTTPS or not.

Upvotes: 3

Related Questions