麻烦用Ruby的'open-uri'打开utf-8 URI [英] Trouble opening utf-8 URI's with Ruby's 'open-uri'
问题描述
尝试获得丹麦的Ærø,丹麦:http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk在Chrome中使用的不是与open-uri:
require'rubygems'
requireopen-uri
require'json'
uri =http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk
response = open(uri)
array = JSON.parse(response)
pp数组
这里产生
/usr/lib/ruby/1.8/uri/common.rb:436:in`split':坏URI(不是URI? ):http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk(URI :: InvalidURIError)
另一种做法似乎是转义字符:
uri =http:// m $ p $ https: $ b array = JSON.parse(response.read)
pp数组
但这样产生任何人都有任何想法可以解决这个问题(获得非转义的反馈或发送utf-8请求)?
/ p>
Ruby版本是1.8.7
只需添加
require'string19'
到第二个例子的顶部,它的作品
I'm trying to get Danish location addresses from google maps web services API with ruby and open-uri.
Trying to get Ærø, Denmark: http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk works in Chrome does not with open-uri:
require 'rubygems'
require "open-uri"
require 'json'
uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk"
response = open(uri)
array = JSON.parse(response)
pp array
Here it yields
/usr/lib/ruby/1.8/uri/common.rb:436:in `split': bad URI(is not URI?): http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk (URI::InvalidURIError)
Another way of doing it seems to be to escape characters:
uri = "http://maps.googleapis.com/maps/api/geocode/json?address=ærø&sensor=false®ion=dk"
uri_escaped = URI.escape(uri)
response = open(uri_escaped)
array = JSON.parse(response.read)
pp array
But this yields an escaped result (which is not sought after :-)
Anyone have any idea what could solve this problem (getting unescaped feedback or sending an utf-8 request)?
Ruby version here is 1.8.7
Figured it out:
Just add
require 'string19'
to the top of the second example and it works
这篇关于麻烦用Ruby的'open-uri'打开utf-8 URI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!