How do I URI::encode a string like:
\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a
To get it in a format like:
%124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A
(as per RFC 1738)
Here's what I've tried:
irb(main):123:0> URI::encode "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a"
ArgumentError: invalid byte sequence in UTF-8
from /usr/local/lib/ruby/1.9.1/uri/common.rb:219:in `gsub'
from /usr/local/lib/ruby/1.9.1/uri/common.rb:219:in `escape'
from /usr/local/lib/ruby/1.9.1/uri/common.rb:505:in `escape'
from (irb):123
from /usr/local/bin/irb:12:in `<main>'
Also,
irb(main):126:0> CGI::escape "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a"
ArgumentError: invalid byte sequence in UTF-8
from /usr/local/lib/ruby/1.9.1/cgi/util.rb:7:in `gsub'
from /usr/local/lib/ruby/1.9.1/cgi/util.rb:7:in `escape'
from (irb):126
from /usr/local/bin/irb:12:in `<main>'
I've looked all about the internet and haven't found (or more likely missed) a way to do this, although I am almost positive that the other day I did this without any trouble at all.
Thanks!
Nowadays, you should use
ERB::Util.url_encode
orCGI.escape
. The primary difference between them is their handling of spaces:CGI.escape
follows the CGI/HTML forms spec and gives you anapplication/x-www-form-urlencoded
string, which requires spaces be escaped to+
, whereasERB::Util.url_encode
follows RFC 3986, which requires them to be encoded as%20
.See this answer for more discussion.
I created a gem to make uri encoding stuff cleaner to use in your code. It takes care of binary encoding for you (added some of the example stuff in the code above).
Run
gem install uri-handler
.It adds the uri conversion functionality into the String class. You can also pass it an argument with the optional encoding string you would like to use (by default sets to encoding 'binary' if the straight UTF-8 encoding fails).
You can use
Addressable::URI
gem for that:It uses more modern format, than
CGI.escape
, for example, it properly encodes space as%20
and not as+
sign, you can read more in wikipedia articleUPDATE: see the comment below Ruby url encoding string
Taken from @J-Rou's comment
I was originally trying to escape special characters on file name only (not on path) from full url string.
ERB::Util.url_encode
didn't work for my use.Based on 2 answers of different SO question, it looks like
URI::RFC2396_Parser#escape
is better than usingURI::Escape#escape
. However, they both are behaving the same to me.