[GIS] Google Geocoding API bug

geocodinggoogle-maps-api-3

am calling a google geocoding api via java, and getting wrong answer ! for example if i run the query below from browser i get the location but if i run it using a rest client i get zero result !!!
is there anything to solve this issue ?

https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR&key=AI.....

this is the answer from Google Chrome or Mozilla :

{
"results" : [
{
"address_components" : [
{
"long_name" : "18",
"short_name" : "18",
"types" : [ "street_number" ]
},
{
"long_name" : "Rue du Nideck",
"short_name" : "Rue du Nideck",
"types" : [ "route" ]
},
{
"long_name" : "Otterswiller",
"short_name" : "Otterswiller",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Bas-Rhin",
"short_name" : "Bas-Rhin",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Grand Est",
"short_name" : "Grand Est",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "67700",
"short_name" : "67700",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "18 Rue du Nideck, 67700 Otterswiller, France",
"geometry" : {
"location" : {
"lat" : 48.7237784,
"lng" : 7.363452799999998
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 48.7251273802915,
"lng" : 7.364801780291501
},
"southwest" : {
"lat" : 48.7224294197085,
"lng" : 7.362103819708497
}
}
},
"partial_match" : true,
"place_id" : "ChIJXwysfE2nlkcRjwlaKN_gmrE",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

and this is the answer for the same address when i use a rest client :

{
"results" : [],
"status" : "ZERO_RESULTS"
}

This is my code , and i am using apache http client version 4.5

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>

as for the code here it is :

        String f = "https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR&key=googlekey";

    // executing the request
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(f);
    HttpResponse response = client.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity());
    System.out.println(responseBody);

Best Answer

Given your comment, I think I found your problem:

Calling

https://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=18+RUE+DE+NIEDECK++++39255+OTTERSWILLER&region=FR

as in your question works. Calling

https://maps.googleapis.com/maps/api/geocode/json?sensor=fa‌​lse&address=18+RUE+D‌​E+NIEDECK++++39255+O‌​TTERSWILLER&region=F‌​R

as in your code example from the comment does not work. Using the syntax highlighting here, you already see that there's an issue in the URL. I don't know why, but you managed to introduce two zero-width spaces between the a and the l of sensor=false.

Remove those, and your code will most likely work. To prevent this happening in the future, look into sanitizing URLs that come from unreliable sources (anything not under your control, e.g. user input or dynamic from other sources).

EDIT: Another likely issue: you don't set any request headers at all. The google API is not a fan of that.

The essential header you are missing is probably accept-language . It should look something like this:

accept-language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

I don't have Java set up here, but adding

request.addHeader("accept-language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");

before you client.execute(request) should do the trick. Obviously you can adapt the values to your liking, in this case I used my (German) browser's default values.

With that header set, the request also passes ARC.