Search code examples
javaspringurlencodehttp-request-parameters

How to encode '%' in request param


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.


Solution

  • 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