Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

url escaping in ruby

There are many discussion about URL escaping in Ruby, but unfortunately I didn't find an appropriate solution.

In general, URI.escape should do the job, but looks like it doesn't support all characters, for example it doesn't escape "[".

URI.parse(URI.escape("1111{3333"))  

works well.

URI.parse(URI.escape("1111[3333"))

raises an exception.

I understand that "[" is not an eligible character in URL according to RFC, but when I enter it into the browser it takes it, and renders the page, so I need exactly the same behavior.

Do know any ready solution for escaping in Ruby?

like image 788
com Avatar asked Jan 03 '12 09:01

com


Video Answer


2 Answers

I typically use

CGI.escape

to escape URI parameters.

require 'cgi'. 

CGI.escape('1111[3333')
=> "1111%5B3333" 
like image 72
randomuser Avatar answered Oct 14 '22 01:10

randomuser


The character [ is a uri delimiter character and does not require escaping.

http://www.ietf.org/rfc/rfc2396.txt section 2.4.3. Excluded US-ASCII Characters

like image 34
user2258832 Avatar answered Oct 14 '22 02:10

user2258832