urldecode in ruby?

Ruby

Ruby Problem Overview


How do I transform

www.bestbuy.com/site/Electronics\Audio\abcat0200000.c=3fid=3dabcat0200000

into its original format?

www.bestbuy.com/site/Electronics/Audio/abcat0200000.c?id=abcat0200000

Is there a urldecode?

Ruby Solutions


Solution 1 - Ruby

A better method is CGI.unescape:
URI.unescape is deprecated

decoded_uri = CGI.unescape(encoded_uri)

Solution 2 - Ruby

Via decode:

require 'uri'
URI.decode(encoded_uri)

Ruby 2.7 and above:

require 'uri'
URI.decode_www_form_component(encoded_uri)

Solution 3 - Ruby

The equivalent of PHP's urldecode in Ruby is CGI::unescape.

Solution 4 - Ruby

Well the thing with =3f and =3d is quoted-printable encoding. Ruby can decode it with the .unpack("M") method.

The backslashes? They're just weird and wrong. It would probably be safe to string-replace them to / since backslash should not be in a URL to begin with.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwefwgewegView Question on Stackoverflow
Solution 1 - RubymerqloveView Answer on Stackoverflow
Solution 2 - RubytokhiView Answer on Stackoverflow
Solution 3 - RubyRobert SpeicherView Answer on Stackoverflow
Solution 4 - RubybobinceView Answer on Stackoverflow