URI::InvalidURIError (URI must be ascii only)
According to RFC 3986 a URI may contain only a subset of ASCII characters.
To provide a valid URI the non-ASCII characters should be escaped:
irb(main):008:0> URI.parse "example.com/city/#{URI.encode('bergstraße')}"
=> #<URI::Generic example.com/city/bergstra%C3%9Fe>
The problem is, that I get this error before hitting any controller,
so I just wonder where can I catch this error to parse and fix the
URL?
The problem is you should not really be catching this error. Your rails server should not be responsible for responding to bad or malformed requests.
While you could attempt to write a piece of middleware to hack around the issue you should instead figure out why the clients are sending requests for an invalid URI.
If they are originating from your own application make sure you are escaping slug columns properly and not just creating urls with string interpolation. The stringex gem or friendly_id are your friends here.
Rails App on Google Cloud: URI must be ascii only
The issue is that you have an acute ó in your url.
If you are using Rails, you can use string#parameterize
Or if plain Ruby, you use i18n gem:
require "i18n"
I18n.transliterate("Olá Mundo!")
=> "Ola Mundo!"
URI must be ascii only when Spreadsheet open file?
You can try
File.open(path, 'rb') do |file|
xlsx = Roo::Spreadsheet.open(file, extension: '.xlsx')
end
URI::InvalidURIError for a URI that's not in my power to encode
I sort of solved my issue:
def unshorten(uri)
begin
response = HTTParty.get(uri, limit: 50)
rescue URI::InvalidURIError => error
bad_uri = error.message.match(/^bad\sURI\(is\snot\sURI\?\)\:\s(.*)$/)[1]
good_uri = URI.encode bad_uri
response = self.unshorten good_uri
end
response
end
I don't feel particularly comfortable fetching the URI from the error message string but it seems there's no other way. Or is there? :)
How can i send Korean string in URL?(Ruby on Rails using HTTParty gem)
As the error message says "URI must be ascii only", you may encode Korean to URI format as below.
require 'uri'
str = "안녕".force_encoding('ASCII-8BIT') # "\xEC\x95\x88\xEB\x85\x95"
URI::encode(str) # %EC%95%88%EB%85%95
Further info lies here: Ruby url encoding string
Related Topics
How to Extend Redcarpet to Support Auto Linking User Mentions
Ubuntu 12.10 - Ruby Gem Rmagick Missing Dependency Issue
Uploading a File to a Website with Ruby/Rails
Installing Ruby Gem Less-Rails on Windows MAChine Using Therubyracer
Sorting a Hash in Ruby by Its Value First Then Its Key
Rails:Runtimeerror - Can't Modify Frozen Array When Running Rspec in Rails
Is There a Bug in Ruby Lookbehind Assertions (1.9/2.0)
Error Running Heckle? 'Current_Code': Undefined Method 'Translate' for Ruby2Ruby
Detecting If This Is an Iframe Load or Direct
What Are Some Good Role Authorization Solutions Used with Authlogic
How to Programmatically Check If a Certificate Has Been Revoked
Installing Libyaml for Ruby on a MAC Osx (Lion)
Setting Mime Type for .Ogv Files in Rails Development Environment
How to Generate a Wsdl Using Ruby
Bundle Install/Error in Installing Libv8 (3.3.10.4) on Rails (Running on Lion)