Tommy Nicholas
Tommy Nicholas

Reputation: 1173

Google Geocoding API HTTP Request denied - Ruby

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?

Upvotes: 1

Views: 926

Answers (1)

Lars Haugseth
Lars Haugseth

Reputation: 14881

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"

Upvotes: 2

Related Questions