1

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?

Maciaz
  • 1,084
  • 1
  • 16
  • 31

1 Answers1

2

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);
amanin
  • 3,436
  • 13
  • 17
  • Worked like a charm. Thank you. :) On a side note tho. I had to import a .jar of org.apache.http that seems to be deprecated. Will that affect my app in any way? – Maciaz Apr 17 '18 at 10:16
  • Is there no new version of this jar? If you update the lib in the future and coded your app with this deprecated jar, some methods of your app may stop working... – dcalap Apr 17 '18 at 10:25