1

I'm trying to write a VBA code that interacts with the Google's Distance Matrix Api, and returns me the distance between two points. The problem is, when I try to use latin letters (like ã), it returns a Invalid Destination error:

    <status>INVALID_REQUEST</status>
    <error_message>Invalid request. Invalid 'destinations' parameter.</error_message>
</DistanceMatrixResponse>

The Url I'm using is this:

https://maps.googleapis.com/maps/api/distancematrix/xml?units=metric&origins=Fortaleza&destinations=São+Paulo&key=" & myAPIK

Do you guys know any way to get around this?

But when I use UTF-8 code instead of the word itself, it returns me the distance that I wanted:

https://maps.googleapis.com/maps/api/distancematrix/xml?units=metric&origins=Fortaleza&destinations=S%C3%A3o+Paulo&key=" & myAPIK

I also already tried putting the language (pt-BR) in the Url, but it didn't work.

1 Answers1

0

You're going to want to look at how the actual web page works.

Open the web page in Chrome, then view the Request Traffic in DevTools (F12), it will be on the network tab.

You'll have to find the request in the sidebar, filtering by XHR will help.

When you find the request itself, it will show you how it's encoded. It's unlikely that Latin is the transfer-encoding parameter, and UTF-8 is probably being filtered before the request is sent via JavaScript normalization of your input. But unless you examine the API in DevTools you wont know for sure.

Also if you're just using QueryString (aka the part of the API parameter in the url) to interact with the API, there is a urlencode probably happening. You can see using this website how this works, with Sao Paulo. And you probably need to 'sanitize' your input using an urlencode function before post/get the API.

Peyter
  • 474
  • 3
  • 14
  • 1
    Thanks, I 'sanitized' and It worked. I used the Worksheetfunction.encodeURL ( as suggested [here](https://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba/3812363#3812363)) in the origins and destinations input. – Alysson Silva Jan 14 '20 at 16:06