Search code examples
ruby-on-railsrubycharacter-encodingspecial-charactersrest-client

REST Client and foreign characters


How does one deal with foreign characters in REST Client? Ie.:

RestClient.get "http://api.example.com/1.0/items.json;q=æøå"

which in Ruby on Rails unfortunately returns:

URI::InvalidURIError in MyController#index
bad URI(is not URI?)

The URL works fine in the browser.


Solution

  • According to its docs:

    If you need to normalize URIs, e.g. to work with International Resource Identifiers (IRIs), use the addressable gem (addressable.rubyforge.org/api/) in your code:

    require 'addressable/uri'
    RestClient.get(Addressable::URI.parse("http://www.詹姆斯.com/").normalize.to_str)
    

    Or you can try to encode/escape the string beforehand:

    http://ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI/Escape.html#method-i-encode