0

I am building an application in springboot. I have this block of code:

String testUrl = "http://example.com/filter1/filter2/Art: Is It Value?";
ExampleResources<Test> exampleResources =
                    restTemplate.exchange(testUrl, HttpMethod.GET, null, new 
                    ParameterizedTypeReference<ExampleResources<Test>>() {
                    }).getBody();

It returns empty list. But if I do a GET request for the url in Postman I am getting the correct results. After testing for some time I found that the question mark("?") is causing issues. How should I deal with this?

gopiariv
  • 454
  • 7
  • 9
  • 1
    Does this answer your question? [Java URL encoding of query string parameters](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters) – Ivar Oct 19 '20 at 08:06
  • 1
    Try `"http://example.com/filter1/filter2/" + URLEncoder.encode("Art: Is It Value?", StandardCharsets.UTF_8)` – Ivar Oct 19 '20 at 08:14
  • Using URI format instead of String solved the issue. I built the uri using UriComponentsBuilder(). – gopiariv Oct 22 '20 at 06:53

1 Answers1

0

I think you can encode problematic part using URLEncoder.encode method.

Look at the below sample code.

String url = "http://example.com/filter1/filter2/";
String value = "Art: Is It Value?";
String newValue = URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
String newUrl = url + newValue;

So your newUrl will look like something below.

http://example.com/filter1/filter2/Art%3A+Is+It+Value%3F

See if that helps resolving your issue.

lkamal
  • 3,788
  • 1
  • 20
  • 34