An existing API I have to call accepts a query parameter in the form
?key=001|ABC|123456
However RestTemplate
, using its internal UriBuilder
is escaping the |
character to %7C
.
Thus the parameter results in
?key=001%7CABC%7C123456
I've tried setting a custom UriTemplateHandler
via setUriTemplateHandler
, using a new DefaultUriBuilderFactory
final var uriBuilder = new DefaultUriBuilderFactory();
and disabling the encoding and parsing
uriBuilder.setEncodingMode(EncodingMode.NONE);
uriBuilder.setParsePath(false);
however another exception is thrown internally when constructing the URI
URI.create(url)
saying an illegal character is present, which is, obviously |
.
How can I totally disable this behavior in Spring, using RestTemplate
?
I cannot use escaped values.
Executing the same call via SoapUI, using a non-escaped URL, returns the correct response.
It seems that there is no clean way to do this with Spring classes.
In the end I've choosen for the raw HttpURLConnection
, which, while not as easy-to-use, provide all the required functionality anyway.
The thing is internally, the DefaultUriBuilderFactory
which is the one used to build the final URI, uses the java.net.URI
class, which obey RFCs rules.
private URI createUri(UriComponents uric) {
if (encodingMode.equals(EncodingMode.URI_COMPONENT)) {
uric = uric.encode();
}
return URI.create(uric.toString());
}
As you can see, even if you disable encoding via
uriBuilder.setEncodingMode(EncodingMode.NONE);
the final result is always a call to URI.create
.
You cannot escape that, if not by providing a custom UriBuilderFactory
.