Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Struggling to post json/xml to Google GeoLocation API...
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff  
View profile  
 More options Jun 22 2011, 8:15 pm
From: Jeff <fsjj...@gmail.com>
Date: Wed, 22 Jun 2011 17:15:11 -0700 (PDT)
Local: Wed, Jun 22 2011 8:15 pm
Subject: Struggling to post json/xml to Google GeoLocation API...
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,"addres s_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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sandro  
View profile  
 More options Jun 22 2011, 8:24 pm
From: Sandro <sandro.turri...@gmail.com>
Date: Wed, 22 Jun 2011 17:24:54 -0700 (PDT)
Local: Wed, Jun 22 2011 8:24 pm
Subject: Re: Struggling to post json/xml to Google GeoLocation API...
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Dyck  
View profile  
 More options Jun 22 2011, 8:49 pm
From: Jeff Dyck <fsjj...@gmail.com>
Date: Wed, 22 Jun 2011 17:49:59 -0700
Local: Wed, Jun 22 2011 8:49 pm
Subject: Re: Struggling to post json/xml to Google GeoLocation API...

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\"}"

(* 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

On 2011-06-22, at 5:24 PM, Sandro wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sandro  
View profile  
 More options Jun 22 2011, 9:01 pm
From: Sandro <sandro.turri...@gmail.com>
Date: Wed, 22 Jun 2011 18:01:01 -0700 (PDT)
Local: Wed, Jun 22 2011 9:01 pm
Subject: Re: Struggling to post json/xml to Google GeoLocation API...
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:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Dyck  
View profile  
 More options Jun 22 2011, 9:15 pm
From: Jeff Dyck <fsjj...@gmail.com>
Date: Wed, 22 Jun 2011 18:15:24 -0700
Local: Wed, Jun 22 2011 9:15 pm
Subject: Re: Struggling to post json/xml to Google GeoLocation API...

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

On 2011-06-22, at 6:01 PM, Sandro wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »