Mar 2011
AUTHORMike Benner



CGI.escape != URI.escape

Ran into an interesting Ruby 1.8.7 issue the other day.  I had some UTF-8 data that needed to be escaped and returned as a query string in a url.  The system was already using CGI.escape so I went with it.  While it claimed to return a UTF-8 encoded string, running it through a parser showed something was not quite right.

Upon digging around the web, I found all kinds of work arounds using Iconv, but non did the trick for me.  Ultimately I found a bug posted for Ruby 1.8 that had a similar description.  The other recommendations had to do with moving to 1.9.2, which is not an option at this point for other reasons.

Ultimately I went to URI.escape and that did the trick.  There are some other differences, but all minor in comparison to getting a properly UTF-8 encoded URL.

Speak Your Mind