ruby on rails - bad uri google places from heroku server -
i have google places api key , able query google places api localhost.
i on rails 3.1, , when pushed site heroku bad request. referring http google places api , not javascript 1 - although suspect problem same either way (see message google below)
here how account configured in google console:
key browser apps (with referers) api key: xxxxxx referers: referer allowed activated on: mar 5, 2013 9:41 pm activated by: xx@stanford.edu –
there no problem when on local host.
however when push app , make query google places get:
bad uri(is not uri?): https://maps.googleapis.com/maps/api/place/textsearch/json?query=blamp&key=xxxxx&sensor=false&types=restaurant|food|bar|cafe
edit: i've pinpointed error because of usage of bars '|' separate google types @ end. works both when copy/paste query browser header, , when in development on localhost. ideas:
1) why happening. 2) how fix it?
here how access api ruby code:
query = uri.escape(params[:search]) url_params = "json?query="+ query + "&key="+ google_api_key + "&sensor="+ false.to_s + "&types="+google_types.join("|") url_params << "&pagetoken="+params[:next_token] if params[:next_token] url = uri.parse("https://maps.googleapis.com/maps/api/place/textsearch/"+url_params) http = net::http.new(url.host, url.port) http.use_ssl = true http.verify_mode = openssl::ssl::verify_none request = net::http::get.new(url.request_uri) result = http.request(request) json.parse(result.body)
a note on google_types: global constant array; join "|" pass url.
the problem when uri.encode string, query not return resutls simple requests "steak house san francisco", whereas before uri.encode did. obvs query becomes useless if can't return proper results did on localhost.
perhaps should encode url. practice encode urls requests , parameters. may of help: uri::invalidurierror (bad uri(is not uri?): ):
edit: url character "|" might not safe, therefore need url-encode such queries.
try this:
def search_google_places api_key = 'xxx' base_url = 'https://maps.googleapis.com/maps/api/place/textsearch/json?key=' + api_key tail_url = '&query=garena&sensor=false&types=restaurant|food|bar|cafe' main_url = base_url + api_key + tail_url url = uri.parse(uri.encode(main_url)) response = net::http.start(url.host, use_ssl: true, verify_mode: openssl::ssl::verify_none) |http| http.get url.request_uri end case response when net::httpredirection # repeat request using response['location'] when net::httpsuccess outputdata = json.parse response.body else # response code isn't 200; raise exception pp response.error! end return outputdata end
Comments
Post a Comment