Mike3355
Mike3355

Reputation: 12061

Get and decode a response from RestTemplate spring boot

I am encoding a URL and when I get the response back it is encoded as well. What I am having a issue with is decoding it.

String encoded = URLEncoder.encode(text,"UTF-8");

        UriComponentsBuilder builder = UriComponentsBuilder
            .fromUriString("https://google.com/translate")
            .queryParam("srcLang", srcLang)
            .queryParam("tgtLang", tgtLang)
            .queryParam("text", encoded);

        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),HttpMethod.GET, request, String.class);

what I was trying to do is:

String decodedResult = UriUtils.decode(response.toString(),"UTF-8");

But that didn't work.

advice?

Upvotes: 0

Views: 2045

Answers (1)

Zishan Mohsin
Zishan Mohsin

Reputation: 827

response object is of ResponseEntity type. You should first get body from response then decode it.

String decodedResult = UriUtils.decode(response.getBody(),"UTF-8");

Upvotes: 1

Related Questions