Search code examples
javaspringgetspring-restcontrollerspring-rest

Spring REST GET request produces hashes


Here's my code to retrieve the JSON string from the specific API:

 RestTemplate restTemplate = new RestTemplate();
 String jsonString = restTemplate.getForObject("http://us.turtlepool.space/api/stats", String.class);

It works for several other sites, but on this specific url it just produces:

HH
nTmâÖë«·ó`N¦ò±t'«SÆe÷âb}
ÆùT4;%g#þj*[Ã<«5·Ì
yTÖ%e¸ìh­e7Sµ,9\ÇX.æâësR|¼oñÏ1"%ºÄÆE[.w¿bâMm¤d×2¦÷\Ê25´ègj.YÜ£×Uñmég1ÖÕ]Æ_3¼M_7f}ö6|i)ÍTæOÚìmH5ç¤fbáã
ê51

What could be wrong?


Solution

  • When querying your URL with firefox, I see that in response headers, I've got "Compression=deflate". That means returned stream is GZIP encoded. So, the returned string is in fact the compressed content, not the json. I find it strange that RestTemplate does not take care of it by default.

    To make your example work, I've followed this answer (tested locally, response look ok) :

    HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                HttpClientBuilder.create().build());
        RestTemplate restTemplate = new RestTemplate(clientHttpRequestFactory);
        String jsonString = restTemplate.getForObject("http://us.turtlepool.space/api/stats", String.class);