Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS decodeURIComponent not working properly

When I tryed to decode the string below in nodeJS using decodeURLCompnent:

var decoded = decodeURI('Ulysses%20Guimar%C3%A3es%20-%20lado%20par');
console.log(decoded);

I got

Ulysses Guimarães - lado par

Instead of

Avenida Ulysses Guimarães - lado par 

But when I use the same code on the client side (browser) I can get the right char 'ã'.

Is there a way to convert from ã to ã in a Node script?

like image 535
nanndoj Avatar asked Feb 07 '14 20:02

nanndoj


People also ask

What is difference between decodeURI and decodeURIComponent?

decodeURI(): It takes encodeURI(url) string as parameter and returns the decoded string. decodeURIComponent(): It takes encodeURIComponent(url) string as parameter and returns the decoded string.

What does decodeURIComponent do in Javascript?

The decodeURIComponent() function decodes a Uniform Resource Identifier (URI) component previously created by encodeURIComponent or by a similar routine.

What is encodeURIComponent?

The encodeURIComponent() function encodes a URI by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character (will only be four escape sequences for characters composed of two "surrogate" characters).


3 Answers

I cannot reproduce it in 0.10 or 0.11 versions of node.

You can convert first to second using new Buffer('Ulysses Guimarães - lado par', 'binary').toString('utf8'), but it's a workaround, not a solution.

Are you sure you're calling decodeURI, not unescape?

like image 108
alex Avatar answered Oct 05 '22 22:10

alex


Use var querystring = require("querystring");

The querystring.unescape() method performs decoding of URL percent-encoded characters on the given str.

and then querystring.unescape(str) as per docs:

https://nodejs.org/api/querystring.html#querystring_querystring_unescape_str

like image 34
kittu Avatar answered Oct 06 '22 00:10

kittu


I'm just leaving this here, because I had the same problem. I was using the encodeURIcomponent(str) function in the client and in Nodejs when I did decodeURI(str) had the same problem. I solved it by using encodeURI(str) at the client.

like image 20
Masiar Avatar answered Oct 05 '22 22:10

Masiar