Forum: Ruby URI.parse and whitespace characters

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3518ff338e5eee9b32eb1815daabc602?d=identicon&s=25 Marcelo Barbudas (Guest)
on 2009-04-13 12:54
(Received via mailing list)
Hi,

I have to parse a lot of bad links like:
http://www.something.com/bad link.jpg
(spaces in them)

URI.parse fails parsing them and considers them as faulty links.

However in my case I need it to work. Is there a workaround for this?
42773b24e0c3fb506a8a875c058a9dd7?d=identicon&s=25 Dylan Evans (Guest)
on 2009-04-13 13:03
(Received via mailing list)
That's not necessarily a bad url, you just need to encode it properly, i
believe %20 is the code for a space.
Google gives me this summary;
http://www.blooberry.com/indexdot/html/topics/urle...
5772c599ccab3081e0fffb1d54f3b6de?d=identicon&s=25 Andrew Timberlake (andrewtimberlake)
on 2009-04-13 13:07
(Received via mailing list)
On Mon, Apr 13, 2009 at 12:54 PM, Marcelo Barbudas <nostef@gmail.com>
wrote:
>
> --
> M.
>
>

uri = "http://www.something.com/bad link.jpg"
URI.parse(uri.gsub(/ /, '+')) #<- replaces all spaces with '+'

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
3518ff338e5eee9b32eb1815daabc602?d=identicon&s=25 Marcelo Barbudas (Guest)
on 2009-04-13 13:34
(Received via mailing list)
Thanks for both the answers.

CGI.escape is my friend:)
E1d641bfe4071a5413bac781f06d3fd1?d=identicon&s=25 Sean O'halpin (sean)
on 2009-04-14 00:24
(Received via mailing list)
On Mon, Apr 13, 2009 at 12:34 PM, Marcelo Barbudas <nostef@gmail.com>
wrote:
> Thanks for both the answers.
>
> CGI.escape is my friend:)
>
> --
> M.
>
>

Just a small point - URI escaping and CGI escaping are similar but not
the same. To get URI escaping, require 'uri' and use URI.escape
instead of CGI.escape.

Regards,
Sean
This topic is locked and can not be replied to.