Best way to urldecode (php) in NodeJS

cyberwombat picture cyberwombat · Apr 6, 2013 · Viewed 12.9k times · Source

So I am trying to decode a string that was previously urlencoded with php in Node. About a month ago I had it working with:

querystring.unescape(str.replace(/\+/g, '%20'));

Then it just stopped working - not sure if it was some Node upgrade or what. After playing around it seems I can just use 'unescape()' but I am not sure it if it's foolproof yet.

unescape(str.replace(/\+/g, '%20'));

My question is what is the best way and has anyone else noticed this issue. Note that the first line works with simple strings but breaks down with odd characters - so maybe some encoding issue I am not seeing.

Here's a string:

%E6.%82%CCI-T%8C%01+A

Now go to http://www.tareeinternet.com/scripts/unescape.html and decode it. That is my original (it's an RC4 encrypted string). I want Node to return that string.

Answer

Golo Roden picture Golo Roden · Apr 6, 2013

If you just use the unescape function that's built in into Node.js, your result should be what you want.

Using Node.js 0.10.1 and running

unescape('%E6.%82%CCI-T%8C%01+A');

on the interactive shell, I get

'æ.ÌI-T\u0001+A'

as result which looks pretty much like what you would like to get.

Hope this helps :-)