2

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 
rkb
  • 3,442
  • 19
  • 25
kunnu
  • 414
  • 7
  • 13

1 Answers1

2

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.

rkb
  • 3,442
  • 19
  • 25
  • Cheers! The thing to do on this site is to accept the answer that works for you: http://stackoverflow.com/faq#howtoask – rkb Jan 10 '12 at 03:35