0

I want to pass request parameter in spring API as

%3 ST%

I send this request via postman like

%253 ST%

This works as expected. The problem is that I need to pass this from one microservice to another via REST API. Both applications are built on Spring framework. When I construct this url as string as

localhost:port/api/version/search?address=%253ST%25

The other microservice endpoint couldn't decode and throws exception

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: "3 "
    at java.net.URLDecoder.decode(URLDecoder.java:194)
    at io.undertow.util.QueryParameterUtils.decodeParam(QueryParameterUtils.java:134)
    at io.undertow.util.QueryParameterUtils.handleQueryParameter(QueryParameterUtils.java:118)
    at io.undertow.util.QueryParameterUtils.parseQueryString(QueryParameterUtils.java:106)
    at io.undertow.util.QueryParameterUtils.mergeQueryParametersWithNewQueryString(QueryParameterUtils.java:151)
    at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImpl(RequestDispatcherImpl.java:205)
    at io.undertow.servlet.spec.RequestDispatcherImpl.forwardImplSetup(RequestDispatcherImpl.java:150)
    at io.undertow.servlet.spec.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:112)
    at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:213)
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:171)
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:394)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)

I am calling the service as

String searchQuery = "%" + searchAddress + "%";
            String uri = MessageFormat.format(GET_SERVICE_ADDRESSES, URLEncoder.encode(searchQuery, "UTF-8"));
            Response response = rootWebTarget.path(uri).request(MediaType.APPLICATION_JSON_TYPE).header("Authorization", "AUTH_TOKEN").get();

I also tried to use URLEncoder to encode and then send this to API. When I encode that search string it looks like

%253+ST%25
or
%253%20ST%25

Both of these encode don't work.

Please help.

user82504
  • 111
  • 1
  • 12
  • 1
    How do you call that other service, which you want to pass query param? Can you post that code? – Nemanja Sep 03 '22 at 09:07
  • I have updated the question with the code that how I am calling the other service. – user82504 Sep 05 '22 at 06:24
  • `URLEncoder.encode(URLEncoder.encode(searchQuery, "UTF-8"), "UTF_8")` - a hack, as the second encode should have been done at forwarding. Also bypassing the HTTP REST protocol is often done. – Joop Eggen Sep 05 '22 at 06:42
  • What type is the rootWebTarget object? With resttemplate this should work without issues. – Nemanja Sep 05 '22 at 08:07
  • WebTarget rootWebTarget ... rootWebTarget represents javax.ws.rs.client.WebTarget. – user82504 Sep 05 '22 at 10:09

3 Answers3

1

Send the query parameter using the queryParam method.

If you submit your request using the code below, that should pass without issues.

  Client client = ClientBuilder.newClient();
        WebTarget rootWebTarget = client.target("localhost:port/api/version");
        rootWebTarget
                .queryParam("address", "%253ST%25") 
                .path("/search") 
                .request(MediaType.APPLICATION_JSON_TYPE)
                .get();

Your request will look like this localhost:port/api/version/search?address=%253ST%25

Nemanja
  • 3,295
  • 11
  • 15
0

I used

String encodedSuffix = URLEncoder.encode(suffix, StandardCharsets.UTF_8.displayName());

And for suffix value %3 ST% I got the result %253+ST%25 and when I send this as a url parameter I saw in debug in my rest controller it was successfully decoded as %3 ST%. So UrlEncoder works as expected

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
0

I am able to resolve the issue with the following changes. I am not sure if it's a hack or something but it's resolved.

            String searchQuery = "%25" + searchAddress + "%25";
            String uri = MessageFormat.format(GET_SERVICE_ADDRESSES, URLEncoder.encode(searchQuery, "UTF-8"));
            Response response = rootWebTarget.path(uri).request(MediaType.APPLICATION_JSON_TYPE).header(HEADER, AUTH_HEADER_VALUE).get();
user82504
  • 111
  • 1
  • 12