0

OK, so my situation is rather simple :

Code :

require 'open-uri'

url = "http://en.wikipedia.org/w/api.php?action=query&format=xml&titles=Italy|France&prop=revisions&rvprop=content";
xml = open(url).read

When titles is set to, e.g. Italy, it works fine, but setting it to Italy|France seems to be causing issues. (supposedly its problem is the | character?)

Here's the error :

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/uri/common.rb:176:in `split': bad URI(is not URI?): http://en.wikipedia.org/w/api.php?action=query&format=xml&titles=France|Italy&prop=revisions&rvprop=content (URI::InvalidURIError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/uri/common.rb:211:in `parse'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/uri/common.rb:747:in `parse'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/open-uri.rb:33:in `open'
    from test.rb:41:in `<main>'

Any ideas?

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223

1 Answers1

1

you can use

encodeURIComponent()

Refer:Unescape special characters correctly from the URL in Rails 3.0.3

Also try:

CGI.escape(url);
Community
  • 1
  • 1
Mani
  • 888
  • 6
  • 19
  • Well, how is this method accessible? I'm not a Ruby guru (just trying it out actually), but this is *not* part of Rails project. It shows an error : `undefined method 'encodeURIComponent' for main:Object (NoMethodError)` – Dr.Kameleon Feb 25 '14 at 04:49
  • Hmmm... Just tried `CGI.escape` just for the `titles` param value, and I think I got it working... :-) – Dr.Kameleon Feb 25 '14 at 04:51