How to handle special characters in API URL - Retrofit - android

I'm using Kotlin and retrofit in my Android App to call API's, but some of the API URLs have these characters in them: ^, #, ~.
I'm using the GET API method. My host URL does not have special / strange characters in them nor does my API method names, but the API methods that I've set up; have a few parameters; and sometimes the arguments I'm sending, when calling the API methods, have these special characters in them.
In those cases I'm not getting a response back and the API call fails.
I've noticed that retrofit changes my URL. It replaces any special character with 3 other characters. I've tested my URLs directly in a browser and in Postman, and they work fine.
Is there something specific I have to do in Kotlin to make retrofit be OK with my special characters?

try this
import java.net.URLEncoder
fun main(args: Array<String>) {
val url = "http://foo bar/"
println(URLEncoder.encode(url, "utf-8")) // note: encodes space to + not %20
}
Output:
http%3A%2F%2Ffoo+bar%2F
You should avoid Special character in Api
URL encoding is often required to convert special characters (such as "/", "&", "#", ...), because special characters:
1. Have special meaning in some contexts, or
2. Are not a valid character for an URL, or
3. could be altered during the transfer.
For instance, the "#" character needs to be encoded because it has a special meaning of that of an HTML anchor.
The character needs to be encoded because it is not a valid URL character. Also, some characters, such as "~" might not transport properly across the internet. Instead of proceeding with the complex process you should focus on correcting the old one.
More you can read here.

Related

Send utf8 character with retrofit as query parameter

How can I send non-ascii characters as a query parameter
without encoding it as ascii with retrofit (assuming thats whats happening)?
For example I dont want the value of the query q to go from /?q=Gä to /?q=G%C3%A4 when it arrives at the server end.
#GET("endpoint/")
fun get(
#Query("q") value: String,
): Response
I have tried using #Query(value = "q", encoded=true) though I dont think that does what I initially thought it would.
I also want to mention that I can send this request in my firefox browser and it works correctly.
Non-ASCII Header Encoding Issues #891 (though about the header)

How can I send special character parameter in URLencoded form without change using volley library

How can I send special character parameter in URLencoded form without change using volley library
I have to send
qty$1:23
qty$2:666
this data in URLencoded form using volley library but due to Volley Request class it is doing UTF encoding foe key value pairs.How can I avoid to change the parameter names.
Using Volley now parameters names are changed like qty%241:23 which is not acceptable by server.Please help me
Try encoding first:
String encoded = URLEncoder.encode(stringToEncode, "UTF-8"); and send the encoded string.
According to the documentation of this method:
This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

Length limit on JSON parameters on GET request

My Android app is making two GET calls to my Server API. In the first one, is this, where parameter code is a 256 char String.
$.getJSON( myServerEndpoint, {
action: "doStuff1",
username: $("#username").val(),
code: my256charString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
The second one is this, where parameter code is a 5120 char String. Both reach the same server endpoint.
$.getJSON( myServerEndpoint, {
action: "doStuff2",
username: $("#username").val(),
code: my5120CharString,
format: "json"
})
.done(function( data ) {
doStuff2Response(data);
});
When I call both of them from the same device and same user connected to WiFi or most mobile data providers, it works perfectly.
However, when I connect from a Vodafone data connection, the second request never reaches the server. I cannot find any other explanation than that there is a limit on the length of the parameters with Vodafone.
Any ideas or solutions?
OK, so here it goes. First, read this: What is the maximum length of a URL in different browsers?
Yes, there's a limit in the length of the "URL", but someway I don't know how to explain why it is happening only for vodafone. Plus, I don't even know how the request pass through their servers anyways.
As for the solution, you should consider changing from GET request to POST request when the payload is too big.
Quick solution: Base64-encode the code part of the message. Downside: you must decode on the server. This is a standard function in most languages though.
If you're already using Base64 or somesuch cypher, what about Blobs? https://developer.mozilla.org/en-US/docs/Web/API/Blob
chromano's suggestion is spot-on too, just switch to POST and you will definitely get an unlimited Post Body. Downside: Have to JSON.stringify and JSON.parse for yourself, and if you want to expose this URL to a user (say as a link to share) it now can't carry the same information (URL's are GET requests).

Cannot find Web1.PostText (and others) in app inventor 2

I'd like to construct an app which lets you search for players in various online databases and which will display the content in webviewer.
While its easy with some services (since the target-url basically is url/nickname), with some services i need the app to enter the nickname in the search field and search for it.
As I was searching for answers I found the following topic
XML-RPC HTTP request with App Inventor?
I'm not quite sure whether this is what I've been searching for, still I'd love to experiment with Web1.PostText and Web1.RequestHeaders, but I cannot find these blocks in App Inventor 2.
take a look at the App Inventor documentation of the web component
RequestHeaders
The request headers, as a list of two-element sublists. The first element of each sublist represents the request header field name. The second element of each sublist represents the request header field values, either a single value or a list containing multiple values.
PostText(text text)
Performs an HTTP POST request using the Url property and the specified text.
The characters of the text are encoded using UTF-8 encoding.
If the SaveResponse property is true, the response will be saved in a file and the GotFile event will be triggered. The responseFileName property can be used to specify the name of the file.
If the SaveResponse property is false, the GotText event will be triggered.

Android: Parsing URL into web service without space character

All I want to do is to send a URL String into my RESTFUL web service with some kind of code like this
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText());
Its all going well until I found error with space character in my URL. I found some solution about replacing the space character with %20 which is I already defined with something like this :
URL someURL= new URL("http://myWebService:port/service/"+CharSequence.getText().replace(" ", "%20"));
Everything, again, seems going well until i found that the replace(Char oldChara, Char newChara) function can only replace ONE space character, and not two.
For brief example when I send the CharSequence.getText() with values "We won" there will be no error, but when I change the values into "We won the battles" there will be an error issuing that there are some illegal character sent to my RESTFUL web service.
Any kind of answer will come up with my great thanks and big salute
~Regards~
Use replaceAll instead of replace.
Although, you should really be doing proper URL encoding. You can use URLEncoder.encode
for example.

Categories

Resources