Question

I'm trying to access the Basecamp API through Rails, but it responds with a SocketError. My code is like this:

require 'rubygems'
require 'net/https'
http = Net::HTTP.new('https://webonise.basecamphq.com')
http.use_ssl = true
http.start do |http|
  req = Net::HTTP::GET.new('/projects.xml')
  req.basic_auth 'username' , 'password'
  resp, data = http.request(req)
end

The response is:

SocketError: getaddrinfo: Name or service not known 
Was it helpful?

Solution

Net::HTTP.new takes a hostname, not a URI, as its first argument. Try calling URI.parse to break up the URI into the parts you want first:

require 'rubygems'
require 'net/http'
uri = URI.parse("https://webonise.basecamphq.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
req = Net::HTTP::Get.new(uri.request_uri)
req.basic_auth 'username', 'password'
resp = http.request(req)
body = resp.body

You'll also have to get the body in the response from the body method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top