Ruby URI :: InvalidURIError: bad URI (not a URI?), Other than encoding

I know this is a common mistake and I am using a well-known solution, but it still gives me the same error:

require 'open-uri' url = "http://website.com/dirs/filex[a]" safeurl = URI.parse(URI.encode(url)) ... 

Gives URI::InvalidURIError: bad URI(is not URI?):

I know its characters [ and ] causing it. But URI.encode does nothing

What am I doing wrong?

+4
source share
2 answers

I think you wanted to require uri, not open-uri. In addition, in accordance with the documentation, you can specify the second parameter with additional characters for encoding. Try to pass [] there.

Code for this solution:

 safeurl = URI.parse(URI.encode(url, "[]")) 
+14
source

I have the same problem. I used this aproach to solve this problem:

 require 'cgi' url = "http://website.com/dirs/filex[a]" safeurl = URI.parse(CGI.escape(url).gsub("%3A", ":").gsub("%2F", "/")) 

Instead of requiring uri, I use the CGI class to remove invalid characters, but CGI avoids everything, including colons and forward slashes, so after I use gsub to return them.

+1
source

Source: https://habr.com/ru/post/1415113/


All Articles