NodeJS decodeURIComponent not working properly
Asked Answered
S

3

6

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?

Summersault answered 7/2, 2014 at 20:56 Comment(0)
D
8

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?

Degrease answered 8/2, 2014 at 0:30 Comment(0)
J
4

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

Jansson answered 14/5, 2018 at 20:57 Comment(0)
T
1

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.

Testimony answered 22/9, 2015 at 7:22 Comment(1)
If you are using encodeURIcomponent you shold use "decodeURIComponent" and not "decodeURI".Patton

© 2022 - 2024 — McMap. All rights reserved.