Search code examples
springspring-bootrestresttemplate

RestTemplate exchange fail on GET call but works on CURL


I have the follow GET call works in CURL

curl -X GET 'https://us.api.blizzard.com/profile/wow/character/kiljaeden/n%C3%B8m?access_token=123&namespace=profile-us&locale=en_US'

But the following Java code gives 404 error

String testurl = "https://us.api.blizzard.com/profile/wow/character/kiljaeden/n%C3%B8m?access_token=123&namespace=profile-us&locale=en_US";

RestTemplate restTemplate = new RestTemplate();

String out = restTemplate.getForObject(testurl, String.class);

Exact error

org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122)
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102)
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63)

Solution

  • The "n%C3%B8m" part is URL-encoded, and RestTemplate is URL-encoding it again, escaping the % symbols, generating a different URL that causes the 404 error.

    If you put the URL right in the code you should use the Java encoding (unicode), in this case use "n\u00F8m" instead of "n%C3%B8m".

    If you need to support the "n%C3%B8m" format in the Java code you can use URLDecoder to URL-decode the String before using with RestTemplate.