retrofit not valid URI - java

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);

Related

encode some key's value of String URL for HTTP Get Request

I need to encode only parameters of the string url.
my string url is like: http://127.0.0.1:8070/app/api/fetchData?channel=abc&param=status:new|addr:null|roomId:Default&group=iPh&reqtype=p1&serialNo=123890&codeId=A1_8uh&type=p
I want to encode value of param(key).I am working on spring boot project.
Please suggest some solution.
If you're making the request from Java, you can encode a string using base64 like this[1]:
String originalInput = "test input";
String encodedString = Base64.getEncoder().encodeToString(originalInput.getBytes());
I guess my first thought would be to encode the parameters that you want this way and then concatenate the whole thing together. What value are you trying to get out of encoding the parameters?
[1] https://www.baeldung.com/java-base64-encode-and-decode

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.

Why is Retrofit2 adding quotes to my POST body?

I have one API request where the POST body is hex-encoded binary data transmitted as plain text. For reasons which I cannot discern, Retrofit2 is adding quotes around the hex-encoded string before adding it to the request, which causes the server to choke on it and complain about a malformed request.
We're in the middle of converting our app from original Retrofit to Retrofit2, and none of the payload generation code has changed at all.
I've been able to work around this problem by using an Interceptor to remove the enclosing quotes from the request body at runtime, but that seems like a really daft hoop to have to jump through and I'd much rather not have the quotes appear in the first place.
My interface looks like this:
public interface SampleApi {
#POST("sample-endpoint")
Call<ApiResponse> postThing(#Body String hexEncodedData,
#Header(HttpHeaders.DATE) String gmtDateStr,
#Header("X-CUSTOM-ONE") long custom1,
#Header("X-CUSTOM-TWO") String custom2);
}
I've experimented with setting the Content-Type header to various values with no apparent effect. I haven't built a custom type converter yet because having to make one of those for a plain old string seems like it shouldn't be necessary.
Can someone tell me what I'm doing wrong?
Double quotes is logical, as retrofit is sending data in json format so double quotes if type String . Try this it might help you .
public interface SampleApi {
#POST("sample-endpoint")
Call<ApiResponse> postThing(#Body RequestBody hexEncodedData,
#Header(HttpHeaders.DATE) String gmtDateStr,
#Header("X-CUSTOM-ONE") long custom1,
#Header("X-CUSTOM-TWO") String custom2);
}
String strRequestBody = "body";
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"),strRequestBody);
Update, for Kotlin the Code_Life code should look like this:
interface SampleApi {
#POST("sample-endpoint")
suspend fun postThing(
#Body presentation: RequestBody,
#Header(HttpHeaders.DATE) gmtDateStr: String,
#Header("X-CUSTOM-ONE") custom1: Long,
#Header("X-CUSTOM-TWO") custom2: String
): Call<ApiResponse>
}
val strRequestBody = "body"
val requestBody = strRequestBody.toRequestBody("text/plain".toMediaTypeOrNull()

Encode URL query parameters

How can I encode URL query parameter values? I need to replace spaces with %20, accents, non-ASCII characters etc.
I tried to use URLEncoder but it also encodes / character and if I give a string encoded with URLEncoder to the URL constructor I get a MalformedURLException (no protocol).
URLEncoder has a very misleading name. It is according to the Javadocs used encode form parameters using MIME type application/x-www-form-urlencoded.
With this said it can be used to encode e.g., query parameters. For instance if a parameter looks like &/?# its encoded equivalent can be used as:
String url = "http://host.com/?key=" + URLEncoder.encode("&/?#");
Unless you have those special needs the URL javadocs suggests using new URI(..).toURL which performs URI encoding according to RFC2396.
The recommended way to manage the encoding and decoding of URLs is to use URI
The following sample
new URI("http", "host.com", "/path/", "key=| ?/#ä", "fragment").toURL();
produces the result http://host.com/path/?key=%7C%20?/%23ä#fragment. Note how characters such as ?&/ are not encoded.
For further information, see the posts HTTP URL Address Encoding in Java or how to encode URL to avoid special characters in java.
EDIT
Since your input is a string URL, using one of the parameterized constructor of URI will not help you. Neither can you use new URI(strUrl) directly since it doesn't quote URL parameters.
So at this stage we must use a trick to get what you want:
public URL parseUrl(String s) throws Exception {
URL u = new URL(s);
return new URI(
u.getProtocol(),
u.getAuthority(),
u.getPath(),
u.getQuery(),
u.getRef()).
toURL();
}
Before you can use this routine you have to sanitize your string to ensure it represents an absolute URL. I see two approaches to this:
Guessing. Prepend http:// to the string unless it's already present.
Construct the URI from a context using new URL(URL context, String spec)
So what you're saying is that you want to encode part of your URL but not the whole thing. Sounds to me like you'll have to break it up into parts, pass the ones that you want encoded through the encoder, and re-assemble it to get your whole URL.

Categories