I want to pass request parameter in spring API as
%3 ST%
I send this request via postman like
%3 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=%3ST%
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
%3 ST%
or
%3 ST%
Both of these encode don't work.
Please help.
CodePudding user response:
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", "%3ST%")
.path("/search")
.request(MediaType.APPLICATION_JSON_TYPE)
.get();
Your request will look like this localhost:port/api/version/search?address=%3ST%
CodePudding user response:
I used
String encodedSuffix = URLEncoder.encode(suffix, StandardCharsets.UTF_8.displayName());
And for suffix value %3 ST%
I got the result %3 ST%
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