How can i use % with RestTemplate - java

I am trying to make a HTTP request to a API with a route like this:
https://example.test/{ID}/get
The problem is, the ID starts with %, something like "%123123123", after the "excute" method, locate in RestTemplate class, this ID's becomes %25123123123, % changes into %25.
More specific, in this line of the method:
URI expanded = getUriTemplateHandler().expand(url, uriVariables);
Here my uriVariables is just a empty Object, and when i manually change the value of expanded variable removing "25", the request works just fine.
Anyone knows how to fix it ?

After Spring 5, the DefaultUriBuilderFactory, used by RestTemplate, defaults is EncodingMode.URI_COMPONENT, as described below:
Expand URI variables first, and then encode the resulting URI component values, replacing only non-ASCII and illegal (within a given URI component type) characters, but not characters with reserved meaning.
If you want your URI to be unencoded like I do, just create a new DefaultUriBuilderFactory with EncodingMode.NONE and add it to your RestTemplate:
DefaultUriBuilderFactory uriBuilderFactory = new DefaultUriBuilderFactory();
uriBuilderFactory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.NONE);
RestTemplate restTemplate = new RestTemplate();
restTemplate.setUriTemplateHandler(uriBuilderFactory);
You can check here for other EncondingModes.

Related

Springboot : Prevent double encoding of % by Resttemplate

Our code uses Asyncresttemplate as follows
String uri = http://api.host.com/version/test?address=%23&language=en-US&format=json
getAysncRestTemplate().getForEntity(uri, String.class);
But %23 is double encoded in Rest template as %2523 and the url becomes
http://api.host.com/version/test?address=%2523&language=en-US&format=json,
But I need to pass encoded string, It doesn't encode if I pass decoded data '#'
How can I send this request without double encoding the URL?
Already tried using UriComponentsBuilder
Avoid Double Encoding of URL query param with Spring's RestTemplate
The uri argument (of type String) passed to the RestTemplate is actually a URI template as per the JavaDoc. The way to use the rest template without the double encoding would be as follows:
getAysncRestTemplate().getForEntity(
"http://api.host.com/version/test?address={address}&language=en-US&format=json",
String.class,
"#"); // (%23 decoded)
If you know you already have a properly encoded URL you can use the method that has a URI as the first parameter instead:
restTemplate.getForEntity(
new URI("http://api.host.com/version/test?address=%23&language=en-US&format=json"),
String.class);
You can avoid this by not encoding any part of it yourself, e.g use # rather than %23

How to encode comma in Spring UriComponentsBuilder?

I have following code:
String convertedBuilder = builder.toUriString();
convertedBuilder = convertedBuilder.replace(",", "\\,");
URI uri = UriComponentsBuilder.fromUriString(convertedBuilder)
.build().toUri();
The idea is to replace a single comma ',' by a '\,' (slash and comma).
Originated URL should be something like
'server-url'?name=te%5C%2Cst for parameter value 'te\,st.
However Spring generates this one:
'server-url'?name=te%5C%252Cst
What am I doing wrong?
Regards,
I had a similar requirement where I had to call a service with a GPS coordinate like this:
http://example.com/path?param1=40.678806%2C-73.943244
UriComponentsBuilder alone would either:
not encode the , (comma) within the parameter value
encode the encoded comma if I already had encoded the parameter with URLEncoder, thus producing the value 40.678806%252C-73.943244
I resolved by explicitly encoding the query parameter with URLEncoder and telling UriComponentsBuilder that the URL was already encoded:
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/path")
.queryParam("param1" , URLEncoder.encode("40.678806,-73.943244", "UTF-8"));
ResponseEntity<String> resp = template.exchange(
builder.build(true).toUri(),
HttpMethod.GET,
null, new ParameterizedTypeReference<String> () {}
);
How to encode comma in Spring UriComponentsBuilder?
UriComponentsBuilder doesn't encode the comma since RFC doesn't require it to be encoded. You will have to encode it manually as suggested by Andreas in the comments.
However Spring generates this one:
'server-url'?name=te%5C%252Cst
No, it doesn't, as also mentioned by Andreas. It encodes the backslash but not the comma. Please fix the question if you need any more information.

How to build a URI using URIbuilder without encoding hash

I have a URI like this:
java.net.URI location = UriBuilder.fromPath("../#/Login").queryParam("token", token).build();
and I am sending it as response: return Response.seeOther(location).build()
However, in the above URI, # is getting encoded to %23/. How do I create a URI with out encoding the hash #. According to official document, a fragment() method must be used to keep unencoded.
URI templates are allowed in most components of a URI but their value
is restricted to a particular component. E.g.
UriBuilder.fromPath("{arg1}").build("foo#bar"); would result in
encoding of the '#' such that the resulting URI is "foo%23bar". To
create a URI "foo#bar" use
UriBuilder.fromPath("{arg1}").fragment("{arg2}").build("foo", "bar") instead.
Looking at the example from docs, I am not sure how to apply it in my case.
The final URI should look like this:
http://localhost:7070/RTH_Sample14/#Login?token=eyJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJodHRwczpcL1wvcnRoLmNvbSIsInN1YiI6IlJUSCIsInJvbGUiOiJVU0VSIiwiZXhwIjoxNDU2Mzk4MTk1LCJlbWFpbCI6Imtpcml0aS5rOTk5QGdtYWlsLmNvbSJ9.H3d-8sy1N-VwP5VvFl1q3nhltA-htPI4ilKXuuLhprxMfIx2AmZZqfVRUPR_tTovDEbD8Gd1alIXQBA-qxPBcxR9VHLsGmTIWUAbxbyrtHMzlU51nzuhb7-jXQUVIcL3OLu9Gcssr2oRq9jTHWV2YO7eRfPmHHmxzdERtgtp348
To construct the URI with fragment use
UriBuilder.fromPath("http://localhost:7070/RTH_Sample14/").fragment("Login").build()
This results in the URI string
http://localhost:7070/RTH_Sample14/#Login
But if you also add query parameters
UriBuilder.fromPath("http://localhost:7070/RTH_Sample14/").fragment("Login")
.queryParam("token", "t").build()
then the UriBuilder always inserts the query params before the fragment:
http://localhost:7070/RTH_Sample14/?token=t#Login
which simply complies to the URL syntax.
Instead of all the hassle of redirecting without encoding the hash value. I changed my code into the following:
java.net.URI location = new java.net.URI("../#/Login?token=" + token);
So the query param above is token appended to URI location. In front-end I am using angular's location.search().token to get capture the query param.
This worked for me. Looking for better answers though. Thanks

Spring RestTemplate getForObject URL not working for Apple iTunes

I created the following simple test to query iTunes:
#Test
fun loadArtist()
{
val restTemplate = RestTemplate()
val builder = UriComponentsBuilder.fromHttpUrl("https://itunes.apple.com/search")
builder.queryParam("term", "howling wolf")
builder.queryParam("entity", "allArtist")
builder.queryParam("limit", 1)
println("\n\nURL ${builder.toUriString()}")
val result = restTemplate.getForObject(builder.toUriString(), String::class.java);
println("Got artist: $result")
}
And the output was unexpected:
URL https://itunes.apple.com/search?term=howling%20wolf&entity=allArtist&limit=1
Got artist:
{
"resultCount":0,
"results": []
}
Pasting the generated URL into a browser does give expected results - artist returned.
https://itunes.apple.com/search?term=howling%20wolf&entity=allArtist&limit=1
Also, hard-coding the query works:
val result = restTemplate.getForObject("https://itunes.apple.com/search?term=howling%20wolf&entity=allArtist&limit=1", String::class.java);
. . the problem only seems to occur for term queries that include spaces.
What went wrong? Other than assemble the URL by hand, how to fix?
Seems like a case of double encoding the whitespace. From the RestTemplate Javadoc:
For each HTTP method there are three variants: two accept a URI
template string and URI variables (array or map) while a third accepts
a URI. Note that for URI templates it is assumed encoding is
necessary, e.g. restTemplate.getForObject("http://example.com/hotel
list") becomes "http://example.com/hotel%20list". This also means if
the URI template or URI variables are already encoded, double encoding
will occur, e.g. http://example.com/hotel%20list becomes
http://example.com/hotel%2520list). To avoid that use a URI method
variant to provide (or re-use) a previously encoded URI. To prepare
such an URI with full control over encoding, consider using
UriComponentsBuilder.
So it looks like getForObject will actually query for https://itunes.apple.com/search?term=howling%2520wolf&entity=allArtist&limit=1 and thus result in an empty result. You can always just replace whitespaces with a "+" in your term or try to make one of those classes skip the encoding process.

retrofit not valid URI

Hi I'm trying to do a simple http get query via Retrofit.
My parameter has some special characters and it seems that the url encoding fails.
Original:
data=[out:json];node["name"~"Karlsruhe"]["place"~"city|village|town"];out body;
correct encoding should look like this:
data=%5Bout%3Ajson%5D%3Bnode%5B%22name%22~%22Karlsruhe%22%5D%5B%22place%22~%22city%7Cvillage%7Ctown%22%5D%3Bout%20body%3B
but Retrofit creates this:
data=[out:json];node[%22name%22~%22Karlsruhe%22][%22place%22~%22city|village|town%22];out%20body;
and this will fail with:
java.lang.IllegalStateException: not valid as a java.net.URI:
http://overpass.osm.rambler.ru/cgi/interpreter?data=[out:json];node[%22name%22~%22Karlsruhe%22][%22place%22~%22city|village|town%22];out%20body;
at com.squareup.okhttp.HttpUrl.uri(HttpUrl.java:336) at
com.squareup.okhttp.internal.http.RouteSelector.resetNextProxy(RouteSelector.java:135)
at
com.squareup.okhttp.internal.http.RouteSelector.(RouteSelector.java:71)
at
com.squareup.okhttp.internal.http.RouteSelector.get(RouteSelector.java:76)
at
com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:321)
at
com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:245)
at com.squareup.okhttp.Call.getResponse(Call.java:267) at
com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:224)
at
com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:195)
at com.squareup.okhttp.Call.execute(Call.java:79) at
retrofit.OkHttpCall.execute(OkHttpCall.java:112)
What can be done here to fix this encoding issue?
Thanks
I am not sure about what the root cause of the encoding error is, but you can work around it with the encoded parameter to the Query notation. Setting the parameter to true tells retrofit the parameter is already encoded, so do not encode again.
In your service interface, add encoded=true to your #Query annotation. Something like --
Call<ResponseBody> getResponse(#Query(value = "data", encoded = true) String data);
Then, encode the parameter yourself before sending to retrofit.
final String encodedData = URLEncoder.encode(data, "UTF-8");
Call<ResponseBody> result = service.getResponse(encodedData);

Categories