Struggling to post json/xml to Google GeoLocation API...

1,097 views
Skip to first unread message

Jeff

unread,
Jun 22, 2011, 8:15:11 PM6/22/11
to HTTParty Gem
Hey all,

Just starting to dabble with Ruby and XML, initial attempts at using
HTTParty have been amazing, so I'm starting to port some of my
clunkier scripts to Ruby... and now I've run into something that's got
me stumped...

I'm trying to submit a request to Google's location API, (Request
format defined at http://code.google.com/p/gears/wiki/GeolocationAPI?redir=1).
I'm currently doing this via curl, with a command like:

curl http://www.google.com/loc/json -H "Pragma: no-cache" -H "Cache-
control: no-cache" -d
'{"version":"1.1.0","host":"maps.google.com","request_address":true,"address_language":"en_GB","wifi_towers":
[{"mac_address": "xx-xx-xx-xx-xx-xx", "signal_strength": -81,"age":
0}]}'

Usually I have multiple WIFI access points defined in the
"wifi_towers" section, but I'm trying to simplify here.

Anyway, I'm trying to port this to Ruby, and what I have so far is:
---------------------------------------------------------------------------------------------------
#!/usr/bin/ruby

require 'rubygems'
require 'httparty'

class GeoLocate
include HTTParty
debug_output $stdout
format :json
base_uri 'http://www.google.com'
headers "Pragma" => "no-cache", "Cache-controle" => "no-cache"
end

queryHash = {}

queryHash['version']='1.1.0'
queryHash['host']='maps.google.com'
queryHash['request_address']=true
queryHash['address_language']='en_GB'
queryHash['wifi_towers']={"signal_strength" => -57, "mac_address" =>
"00-24-6c-a9-01-51"}

result = GeoLocate.post('/loc/json',:body => queryHash)
---------------------------------------------------------------------------------------------------

If I pp the queryHash, that looks like what I'm expecting:

{"address_language"=>"en_GB",
"version"=>"1.1.0",
"request_address"=>true,
"host"=>"maps.google.com",
"wifi_towers"=>{"signal_strength"=>-57, "mac_address"=>"00-24-6c-
a9-01-51"}}

However, when I post that to Google, I get the following response:

---------------------------------------------------------------------------------------------------

opening connection to www.google.com...
opened
<- "POST /loc/json HTTP/1.1
Cache-Controle: no-cache
Content-Type: application/x-www-form-urlencoded
Connection: close
Content-Length: 154
Host: www.google.com
Pragma: no-cache

"
<-
"address_language=en_GB&version=1.1.0&request_address=true&host=maps.google.com&wifi_towers[signal_strength]=-57&wifi_towers[mac_address]=00-24-6c-
a9-01-51"
-> "HTTP/1.1 400 Bad Request"
-> "Content-Type: text/plain"
-> "Date: Thu, 23 Jun 2011 00:12:02 GMT"
-> "Expires: Thu, 23 Jun 2011 00:12:02 GMT"
-> "Cache-Control: private, max-age=0"
-> "X-Content-Type-Options: nosniff"
-> "X-Frame-Options: SAMEORIGIN"
-> "X-XSS-Protection: 1; mode=block"
-> "Server: GSE"
-> "Connection: close"
-> ""
reading all...
-> "JSON parsing error.
"
read 20 bytes
Conn close
#<Net::HTTPBadRequest:0x101235b98>
JSON parsing error.

---------------------------------------------------------------------------------------------------

I suppose I could just run curl in the background to get the results,
but I'd really like to start using Ruby object instead of just
dropping to bash commands all the time, not to mention I love how easy
it is to parse the results once HTTParty sorts things out.

Anyway, forgive me if my newby status is making me miss something
glaringly obvious, I'm really hoping that someone out there has an
idea of what I'm missing though.

Thanks in advance,

Jeff

Sandro

unread,
Jun 22, 2011, 8:24:54 PM6/22/11
to HTTParty Gem
Are you supposed to be posting JSON? If so, you'll want to do
something like result = GeoLocate.post('/loc/json',:body =>
JSON.dump(queryHash))

On Jun 22, 6:15 pm, Jeff <fsjj...@gmail.com> wrote:
> Hey all,
>
> Just starting to dabble with Ruby and XML, initial attempts at using
> HTTParty have been amazing, so I'm starting to port some of my
> clunkier scripts to Ruby... and now I've run into something that's got
> me stumped...
>
> I'm trying to submit a request to Google's location API, (Request
> format defined athttp://code.google.com/p/gears/wiki/GeolocationAPI?redir=1).
> I'm currently doing this via curl, with a command like:
>
> curlhttp://www.google.com/loc/json-H "Pragma: no-cache" -H "Cache-
> control: no-cache" -d
> '{"version":"1.1.0","host":"maps.google.com","request_address":true,"addres s_language":"en_GB","wifi_towers":
> opening connection towww.google.com...
> opened
> <- "POST /loc/json HTTP/1.1
> Cache-Controle: no-cache
> Content-Type: application/x-www-form-urlencoded
> Connection: close
> Content-Length: 154
> Host:www.google.com
> Pragma: no-cache
>
> "
> <-
> "address_language=en_GB&version=1.1.0&request_address=true&host=maps.google .com&wifi_towers[signal_strength]=-57&wifi_towers[mac_address]=00-24-6c-

Jeff Dyck

unread,
Jun 22, 2011, 8:49:59 PM6/22/11
to httpar...@googlegroups.com
Wow, thanks for the quick response Sandro, you definitely got me to the next phase.  

Changing the "result = ...." line to:

result = GeoLocate.post('/loc/json', :body => JSON.dump(queryHash))

I now get a result in the debug log, but not into the "result" variable... changing the line again to 

result = GeoLocate.post('/loc/json', :body => JSON.dump(queryHash)).body

does get me the result, but as a string not a hash or array, so it's not getting parsed by HTTParty.

The result I get looks like:

"{\"location\":{\"latitude\":##.#######,\"longitude\":-###.########,\"accuracy\":64.0},\"access_token\":\"2:Mkgg_dsFWPniHolX:4nG3UXVLT27ndr4P\"}"

(* Lat and Long  results redacted)

Any more ideas?  I suppose I could start working on parsing it out manually, but definitely nicer if HTTParty does it for me (I'm lazy I guess)

Jeff

Sandro

unread,
Jun 22, 2011, 9:01:01 PM6/22/11
to HTTParty Gem
The body method returns the Net::HTTP body, that is, the raw body
before parsing takes place. You can call #parsed_response to get the
parsed body but you really shouldn't have to - just work with the
HTTParty::Response object. Try doing something like result.keys or
puts(result.to_yaml) or result.code or result.not_found? At this
point, unless there was a parse error, those methods should all work.



On Jun 22, 6:49 pm, Jeff Dyck <fsjj...@gmail.com> wrote:
> Wow, thanks for the quick response Sandro, you definitely got me to the next phase.  
>
> Changing the "result = ...." line to:
>
> result = GeoLocate.post('/loc/json', :body => JSON.dump(queryHash))
>
> I now get a result in the debug log, but not into the "result" variable... changing the line again to
>
> result = GeoLocate.post('/loc/json', :body => JSON.dump(queryHash)).body
>
> does get me the result, but as a string not a hash or array, so it's not getting parsed by HTTParty.
>
> The result I get looks like:
>
> "{\"location\":{\"latitude\":##.#######,\"longitude\":-###.########,\"accur acy\":64.0},\"access_token\":\"2:Mkgg_dsFWPniHolX:4nG3UXVLT27ndr4P\"}"

Jeff Dyck

unread,
Jun 22, 2011, 9:15:24 PM6/22/11
to httpar...@googlegroups.com
OK, that does the trick, I suspect if I wasn't such a newbie at this it probably would have been glaringly obvious.  Thanks for the help!!

Jeff
Reply all
Reply to author
Forward
0 new messages