function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Douglas GDouglas G 

Google Maps Geocode api not seeing my HTTP GET Params

Hey all apologies, Im certain thius is something elementary but Ive been going round it for ages now and losing my tiny mind. 

Im working on a custom lightning componsnt and need to geocode adresses to put them on a map. I beleive Im correct in thinking that lightning wont let me call the geocoding WS directly from the JS and instead need to do this in apex. 

Issue Im having is I cant even get a dead simple request to googles geocode to work correctly, instead as below it returns me an error - Invalid request. Missing the 'address', 'bounds', 'components', 'latlng' or 'place_id' parameter ie not seeing the address param Im setting using req.setHeader('address', addrEncode  );. Via Postam or browser this works fine.

Apex Class
@AuraEnabled
    public static void testGeocodeWS() {
        
        System.debug('testGeocodeWS');
        String endpoint = 'https://maps.googleapis.com/maps/api/geocode/json';
        String addrRaw = 'SAPPHIRE HOUSE UNIT 2, CRISTAL BUSINESS CENTRE , 47 KNIGHTSDALE ROAD , IPSWICH , IP1 4JJ';
        String key = 'XXX OBSCURED FOR POST XXX';
        
        String addrEncode  = System.EncodingUtil.urlEncode(addrRaw, 'UTF-8' );
        
        System.debug('addrEncode = ' + addrEncode);
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(endpoint);
        req.setMethod('GET');
        req.setHeader('address', addrEncode  );
        req.setHeader('key', key );
        
        System.debug('req = ' + req);
        System.debug('req.getEndpoint() = ' + req.getEndpoint());
        System.debug('req.getHeader(\'key\') = ' + req.getHeader('key'));
        System.debug('req.getHeader(\'address\') = ' + req.getHeader('address'));
        
        HttpResponse res = h.send(req);
        
        System.debug('res = ' + res);
        System.debug('res.getBody() = ' + res.getBody());

    }

And the logs im getting from my multitude of debug statements are:  (sorry for it being an image) 

User-added image
Douglas GDouglas G
.... And now resolved by setting the endpoint like the below. 
 
req.setEndpoint('https://maps.googleapis.com/maps/api/geocode/json?address=' + addrEncode + '&sensor=false&key='+key);

clearly req.setHeader('address', addrEncode  ); didnt work like I imagined. Oh well!