Uri::Invalidurierror (Uri Must Be Ascii Only)

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? :)



Related Topics



Leave a reply



Submit