phwd
phwd

Reputation: 19995

Executing requests in ruby using net/http

I am trying to use net/http to get a response for API call based on an old library I dug up https://github.com/jurisgalang/facebook/blob/master/lib/facebook/graph-object.rb#L22

require 'net/http'

API_HOST       = "graph.facebook.com"
API_BASE_URL   = "https://#{API_HOST}"
path           = "/boo"

uri            = URI.parse "#{API_BASE_URL}#{path}"
http           = Net::HTTP.new(uri.host, uri.port)
res            = http.get(uri.request_uri, nil)

The uri ends up as <URI::HTTPS:0x0000010091fc48 URL:https://graph.facebook.com/boo>

This results in

NoMethodError: undefined method `keys' for nil:NilClass

I assumed it is because dest argument is obsolete: http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-i-get

So I did it without

res            = http.get(uri.request_uri)

Which results in

NoMethodError: undefined method `empty?' for

#<URI::HTTPS:0x0000010091fc48 URL:https://graph.facebook.com/boo>

How can one request a response using net/http and http.get?

Upvotes: 3

Views: 10969

Answers (1)

Matt
Matt

Reputation: 10564

Just at a very quick glance of the documentation, the second argument to get is a hash, and you're passing nil, see:

http.get(uri.request_uri, nil)

Your second attempt should be OK, though I did find that with my setup (ruby 1.9.3 and MacPorts' openssl library) that Net::HTTP was rejecting Facebook's ssl certificate (I was getting "Connection reset by peer". The following code snippet worked for me:

require 'net/http'

API_HOST       = "graph.facebook.com"
API_BASE_URL   = "https://#{API_HOST}"
path           = "/boo"

uri            = URI.parse "#{API_BASE_URL}#{path}"
http           = Net::HTTP.new(uri.host, uri.port)
http.use_ssl   = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
res            = http.get(uri.request_uri)
puts res.body

Note that I'm not verifying the SSL certificate, which is a potential security problem. As a proof of concept, however, this snippet worked for me - so hopefully this will give you a good data point in your debugging effort.

Upvotes: 8

Related Questions