Skip to content
Advertisement

Unescaped Characters when using languages other than English in Node JS?

I am using a Google cloud function with the Google Places API. It works fine in English, but I get this error when inputting Hebrew or Russian:

Unhandled error TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters

When I try my request directly in a web browser it works fine:

https://maps.googleapis.com/maps/api/place/autocomplete/json?key=myKey&type=address&input=основной

But when I try the exact same string in NodeJS I get the error above, here is my code (I tried unescape):

var input = unescape(data.textInput); //string
var key = "myKey";
var type = "address";
var url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?" + "key=" + key + "&type=" + type + "&input=" + input;

Answer

Try to encode the url before performing the actual request.

var url = "https://maps.googleapis.com/maps/api/place/autocomplete/json?" + "key=" + key + "&type=" + type + "&input=" + input;
var encodedUri = encodeURI(url)
Advertisement