8

I have some content (html) that is being encoded as a result of this javascript (from this page) and sent to my rails application:

function encode_utf8_b64(string) {
return window.btoa(unescape(encodeURIComponent(string)));
}

The correspond js code to get it back to original is this:

function decode_utf8_b64(string) {
return decodeURIComponent(escape(window.atob(string)));
}

My question is, is there an equivalent in ruby of decodeURIComponent()? So far I have this that gets it part of the way out, but I'm missing the last step of decodeURIComponent:

CGI::escape(Base64.decode64(string))

1 Answer 1

19

URI.unescape could probably help:

def decode_utf8_b64(string)
  URI.unescape(CGI::escape(Base64.decode64(string)))
end

you have to add the necessary rubygem too:

require 'uri'

I've tested this on ruby 1.9.2.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks - when I apply URI.unescape or URI.decode, I get an "invalid byte sequence in UTF-8" error. It seems that the output of the CGI::escape() is US-ASCII. When I tried to use Iconv to convert the string to UTF-8 before applying URI.unescape, it stopped throwing the error but seemed to just remove one of the deencoding steps so the output was still a mess. Any tips? Or maybe this is a separate issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.