1

My application needs to redirect my user to a external help page, passing the client name as parameter, but this external help page uses a different encoding.

My original string is

"Diagnósticos da América"

and the page expects a String like

"Diagn%F3sticos%20da%20Am%E9rica"

I need:

"Diagnósticos da América" => "Diagn%F3sticos%20da%20Am%E9rica"

I'm trying in irb:

"Diagnósticos da América".encode("ISO-8859-1") => "Diagn\xF3sticos da Am\xE9rica"
"Diagnósticos da América".encode("UTF-8", "Windows-1252") => "Diagnósticos da América"
"Diagnósticos da América".encode("Windows-1252") => "Diagn\xF3sticos da Am\xE9rica"

Does someone have an idea?

  • 1
    Possible duplicate of [In Ruby/rails, how can I encode/escape special characters in URLs?](http://stackoverflow.com/questions/4967608/in-ruby-rails-how-can-i-encode-escape-special-characters-in-urls) – ventiseis Mar 16 '16 at 18:33
  • Welcome to Stack Overflow. Please take the time to format your question for readability. SO provides formatting options for text in questions, answers , and limited formatting in comments. Those help make the text more readable. Also, SO is a reference book, not a discussion, so we don't use salutations, valedictions ("Thanks") or signatures. I'd recommend reading http://catb.org/esr/faqs/smart-questions.html as it's full of great information about asking questions. – the Tin Man Mar 16 '16 at 18:35
  • The answerts to [this question about 'url_encode in Ruby'](http://stackoverflow.com/questions/13338672/url-encode-in-ruby) might be helpful, too. – ventiseis Mar 16 '16 at 18:46

1 Answers1

1

I use this:

URI.encode("Diagnósticos da América".encode('iso-8859-1'))

I've got the expected result:

"Diagn%F3sticos%20da%20Am%E9rica"
  • 1
    When answering, it's a really good idea to provide sample output and explain why your solution is THE solution. Tossing code out helps a little but explaining/teaching provides the true long-term answer and helps those who are searching for similar solutions in the future. Also, does this provide hex-encoded spaces as required? – the Tin Man Mar 17 '16 at 16:34