+ Start a Discussion
Antonio GiraffaAntonio Giraffa 

Google Maps API and 400 error

Hello, I'm having troubles trying to get data from google maps api, specifically distance  matrix service.

The problem is that every time i send the request via salesforce i got a 400 error, BUT if I take the url of the endpoint from the logs and paste it in a browser the json is returned regularly:

Here is the code that I'm using: Am I missing something? Thank you in advance.

 

public String getJsonResults(
            String address1,
            String address2) {
        
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
				
                
         String url = 'https://maps.googleapis.com/maps/api/distancematrix/json'
            + '?key=......'
            + '&origins=' + address1
            + '&destinations=' + address2
            + '&mode=driving'
            + '&sensor=false'
            + '&language=en'
            + '&units=imperial';
            System.debug('url:   ' + url);
                
        request.setEndpoint(url);
        request.setMethod('GET');
        HttpResponse response = http.send(request);


that's what i get from system.debug when I check for the request:
System.HttpRequest[Endpoint=https://maps.googleapis.com/maps/api/distancematrix/json?key=......&origins=Via Celano,Avezzano&destinations=Via Roma,Aquila&mode=driving&sensor=false&language=en&units=imperial, Method=GET]
If i paste this string in a browser, it works. 

Already added the endpoint "https://maps.googleapis.com" to remote sites, what should I do? Thanks again

Best Answer chosen by Antonio Giraffa
Ramesh DepaiahRamesh Depaiah
Make sure space has been replaced with '%20' for both address1 and address2 parameters, when passing space in the url it will throw 400 Bad request error.

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D

All Answers

Ramesh DepaiahRamesh Depaiah
Make sure space has been replaced with '%20' for both address1 and address2 parameters, when passing space in the url it will throw 400 Bad request error.

I hope you find the above solution helpful. If it does mark as best answer to help others too.
Thanks,
Ramesh D
This was selected as the best answer
Raj VakatiRaj Vakati
Change your code like below
 
public String getJsonResults(
            String address1,
            String address2) {
        
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        HttpRequest request = new HttpRequest();
				
                
         String url = 'https://maps.googleapis.com/maps/api/distancematrix/json'
            + '?key=......'
            + '&origins=' + address1
            + '&destinations=' + address2
            + '&mode=driving'
            + '&sensor=false'
            + '&language=en'
            + '&units=imperial';
            System.debug('url:   ' + url);
                
        request.setEndpoint(EncodingUtil.urlEncode(url));
        request.setMethod('GET');
        HttpResponse response = http.send(request);

 
Antonio GiraffaAntonio Giraffa

Thank you to all, I solved the problem with:
 

String test = url.replace(' ', '%20');

Many Thanks!!