Search code examples
javaspringrestcharacter-encodingresttemplate

Java-Spring: REST GET request returns something wrong in body


I am trying to get Json from this url: https://meduza.io/api/v3/search?chrono=news&page=0&per_page=10&locale=ru with pretty simple code:

public Boolean getData(String apiUrl) {
        try {
            //disable SSLCertificateValidation
            SSLCertificateValidation.disable();

            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.add("Content-Type", "application/json;charset=UTF-8");
            headers.add("Accept", "*/*");
            
            ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiUrl, String.class, headers);

            if (responseEntity.getStatusCode().toString().equals("200")) {
                theLogger.info("Server successfully answered with response code: {} - {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
                return true;
            } else {
                theLogger.error("Something goes wrong! Server answered with response code: {} and error: {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
                return false;
            }

        } catch (Exception theException) {
            theException.printStackTrace();
            theLogger.error("Exception: " + theException);
            return false;
        }

    }

It works but I see this in the body:

Server successfully answered with response code: 200 - ��������Y�n�}B� DiF�d�@P�(v����E�]�J�5CNH��b�<Fh_.O�s93��*v]���cs�s/��!�k'5y.S����ԑ�����Q<�Gq0���9%4/�q2����+��Ϋ�𒧹P��~!<�����[�IL[K+���K������J�Eb*�]nV�ݒ;��*U3�y��t���27+iu!5$�Fx�G|����ϰ#a��^T9�x"Ty�Q�V��ܕf)]�Lj%�-���7�PL8'�6��%��~��
����`�T��F;��D���
�F��3��~A����&�+a����xc'X���
��\�
Y�V��5�L

O��0|�fP�Ⱦ<��`@;s��d)�u~�u�9����ngV������ʁ���~�^��F��OM(�l��`E�����^�'G�h��*�cX�E<>�I#,��Q�U���>V&l�4[+��&BkIªF��Ze�~�dnPKe/Z��俉�t���x�j(6�݄b;P�
|cA�    ��΢;�Oի�=$�3:{g��L�G�U��* az���x�`E,��Q��)�̅4%t���f�W�K�$������}�1���Kl�SjV;C�9Ė�DU�oi|��M;��$-@��'�.��GyG���}0w|���|=����3I8����f-0��,������l�[��g��dr�V?.���p<�y�`�m_7x�A�鱯�n��ǿ�k��C+    G�F��J
�&�Nlo8RIq�J@���[\����Y�$�� ��r{X�+��2\�����l���sUȆ���OZ��E�!D�hi�N�L��G��6~-�T����_Azm��s:x��ķ��h��
���[p��1�~ ٭����J���f���۝��1���k����s�R��{U��`�c��et����=��R�.A��Q���{M�Ee���D���Jq��ROƏ�j���2�~�Lb���~Z��F��{p���T��~E���nG�~X���r�x?|�M�����d����e9�~`�sy/а�v��b���(�����N�x4���o��   ?�F����ew��2��[�Yv��ǣi|�4�n�OEY�]t�+�U(�ƛSv!�M��`}��?ޥB���A)�!�mo#>��s۽x(im��*b�����ī�W�C��a����G$BͻӖ]v�f"N�g�#͆�L�o>yc_���?�
DMtꭘ�T�l�Ӹ��b����LցL�?9��������w����.�.K��J��gV�/�O���鮦����~�ZT��]��a
h�i`��z��;p'��X�U��g��`�&mA�`�<Ws��a��#�C��t�K2t��z",���
����ƛ&�"N`\��C�=`�B8�������oFJ%��

What is this? What did I do wrong?


Solution

  • The response is compressed (gzipped) by the server. Most browsers/tools handle compressed responses transparently. Here you have to do it explicitly.

    To uncompress the response you can use GZipInputStream.

    ResponseEntity<byte[]> response = restTemplate.getForEntity(apiUrl, byte[].class);
    GZIPInputStream gzip = new GZIPInputStream(new ByteArrayInputStream(response.getBody()));
    
    // convert the stream to string
    StringWriter writer = new StringWriter();
    IOUtils.copy(gzip, writer, "UTF-8");
    String responseString = writer.toString();
    

    Stream to string conversion uses Apache Commons-IO.

    It seems that the server always uses compression - even if the client does not tell it to do so (by sending the accept-encoding header).

    Examine the value of the Content-Encoding header to test if the response is compressed or not.

    response.getHeaders().get("Content-Encoding"); // contains [gzip] if response is compressed