utf 8 - Rails: How to send a http get request with UTF-8 encoding -
i'm trying use external api information
address=self.address response = httparty.get("http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a") decode_response = activesupport::json.decode(response)
however, address
in chinese, need convert utf-8 code, if not i'll uri::invalidurierror (bad uri(is not uri?):
how it?
i tried address=self.address.force_encoding('utf-8')
, not work, maybe should use other method instead?
update:
uri = "http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a" encoded_uri = uri::encode(uri) response = httparty.get(encoded_uri) decode_response = activesupport::json.decode(response) self.latitude = decode_response['result']['location']['lat']
and can't convert httparty::response string
. what's wrong it?
i found here, think i'll need tell httparty explicityly parse json?
you save file 'geo.rb' , run ruby geo.rb
require 'uri' require 'httparty' address = "中国上海" uri = "http://api.map.baidu.com/geocoder/v2/?address=#{address}&output=json&ak=5dfe24c4762c0370324d273bc231f45a" encoded_uri = uri::encode uri puts httparty.get(encoded_uri)
Comments
Post a Comment