当我将Google地理编码API粘贴到我的浏览器中时,会正确地发出以下(和其他类似的)HTTP请求
http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false
当我在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
我得到了
{
"results" : [],
"status" : "REQUEST_DENIED"
}
这与我未设置“sensor”属性时遇到的错误相同,但这不是问题。
有什么建议吗?
答案 0 :(得分:1)
您需要使用#request_uri
代替#path
,否则您的查询参数将不会包含在内:
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"