1

I have tried to use RestTemplate for consuming a REST api. The response is not getting parsed or displayed in the case of response is using utf-16 encoding. I have tried different options but failed to get the response. Please find the response headers and the response string.

Response Headers {Date=[Sat, 30 Oct 2021 17:24:03 GMT], Content-Type=[application/json; charset=utf-16], Transfer-Encoding=[chunked], Connection=[keep-alive], Access-Control-Expose-Headers=[], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[POST], Access-Control-Allow-Headers=[authorization,Access-Control-Allow-Origin,Content-Type,SOAPAction,apikey,testKey,Authorization], CF-Cache-Status=[DYNAMIC], Expect-CT=[max-age=604800, report-uri=""], Server=[cloudflare]}

Response String : ????????????????????????????????????????????????????????????????????

Code :

        restTemplate = new RestTemplate();
        StringHttpMessageConverter msgConv = new 
        StringHttpMessageConverter(Charset.forName("UTF-8"));
        restTemplate.getMessageConverters().add(0, msgConv);
        System.out.println("CONV " + msgConv.getDefaultCharset());
        HttpHeaders httpHeaders = new HttpHeaders();
        //httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.set("Accept", MediaType.APPLICATION_JSON_VALUE);
        for (Entry<String, String> headerEntry : headers.entrySet()) {
            httpHeaders.add(headerEntry.getKey(), headerEntry.getValue());
        }
ResponseEntity<String> postResultBody = restTemplate.exchange(prepareUrl(path), method, httpEntity, String.class, parameters);
vineethpj
  • 13
  • 1
  • 3

0 Answers0