1

The following (and other similar) HTTP request to the Google geocoding API comes out correctly when I paste it into my browser

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

And when I do the following in Ruby

url = URI.parse('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

I get

{
"results" : [],
"status" : "REQUEST_DENIED"
}

This is the same error I get when not setting the "sensor" property for example, but that's not the problem.

Any suggestions?

Tommy Nicholas
  • 1,133
  • 5
  • 20
  • 31

1 Answers1

1

You need to use #request_uri instead of #path, or your query parameters will not be included:

url = URI.parse('http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false')

url.path
# => "/maps/api/geocode/json"

url.request_uri
# => "/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"
Lars Haugseth
  • 14,721
  • 2
  • 45
  • 49
  • You're a gentleman and a scholar! The SO question from which I got the HTTP request code even had a comment with that as a caveat, and guess who ignored it? This guy! Thanks Lars. – Tommy Nicholas Aug 26 '13 at 18:21