0

I am getting error when I hit a URL which contains special characters in query parameters using RestTemplate.

My URL is something like this

 https://someendpoint?q=countryCode:(AB+yz)&q=type.code:12345&q=banner.code:A1&q=openDate[*+TO+NOW%2B3MONTH]%20&q=!date:[*+TO+NOW]

Can someone please help me how to encode such type of URL using RestTemplate. This URL works fine when I hit through Browser or Postman.

Pankaj
  • 2,220
  • 1
  • 19
  • 31

1 Answers1

0

You just need to add the StringHttpMessageConverter.

  RestTemplate template = new RestTemplate();
  template.getMessageConverters()
    .add(0, new 
StringHttpMessageConverter(Charset.forName("UTF-8")));
 ResponseEntity<Object> response = template.exchange(endpoint, method, entity, 
                                                Object.class
Prasad
  • 1,089
  • 13
  • 21
  • Its still giving error. It says 500 internal server error (most probably url encoding error.) – Aamir Sheraz Aug 16 '20 at 09:11
  • @AamirSheraz: Have you tried this UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url) .queryParam("msisdn", msisdn) .queryParam("email", email) .queryParam("clientVersion", clientVersion) .queryParam("clientType", clientType) .queryParam("issuerName", issuerName) .queryParam("applicationName", applicationName); – Prasad Aug 16 '20 at 10:20