detailedError":{"code":"BadRequest","message":"Invalid request: invalid position: cannot parse position."},"httpStatusCode":400}]

I am trying to use reverse geocoding using the following end point: https://api.tomtom.com/search/2/reverseGeocode/crossStreet/37.8328%2C-122.27669.json?key=<api_key>. Although this works when passed in browser but shows the following error when I make restTemplate call using spring boot:
org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request: [{"errorText":"Invalid request: invalid position: cannot parse position.","detailedError":{"code":"BadRequest","message":"Invalid request: invalid position: cannot parse position."},"httpStatusCode":400}]

This is the piece of code I am using:
@GetMapping("/revgeocode/data")
public Addresscontainer reverseGeoCode(@RequestParam String position)

        {

            Addresscontainer res = restTemplate.getForObject("https://api.tomtom.com/search/2/reverseGeocode/crossStreet/37.8328%2C-122.27669.json?key="+ apiKey, Addresscontainer.class);

            return res;

        }`   

I am just a beginner. Please help.
Thanks in advance.

Hi Arnob,
That is great that you are playing with TomTom API.
The issue you described may depend on your SpringBoot version. It regards the url encoding. I can see that the url String that you pass to the RestTemplate.getForObject method is already encoded. I think that you get this massage because it might be coded again somewhere in the http client.

The basic usage that works is:
String uriString = "https://api.tomtom.com/search/2/reverseGeocode/crossStreet/37.8328,-122.27669.json?key="; String forEntity = new RestTemplate().getForObject(uriString, String.class);

Since you make that working as expected you can move on with object mappings.

Hope it helps!

1 Like