Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby URI::InvalidURIError: bad URI(is not URI?) besides encoding

I know this is a common error and i'm using a known solution but its still giving 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 the [ and ] characters causing this. But the URI.encode isn't doing anything

What am I doing wrong?

like image 265
Tarang Avatar asked Dec 06 '22 14:12

Tarang


1 Answers

I think you meant to require uri, not open-uri. Also, according to the documentation you can specify a second parameter with extra characters to encode. Try passing [] there.

The code for this solution would be:

safeurl = URI.parse(URI.encode(url, "[]"))
like image 69
John Watts Avatar answered Feb 22 '23 23:02

John Watts