Search code examples
javaandroidjsongetretrofit2

Retrofit adds random number in query parameter?


I am calling a GET API which takes a query parameter, "par", the query parameter contains an encrypted string. The encrypted string contains a JSON data, the issue occurs when the API is called, random strings are added. For instance, this is an encrypted string

"0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D"

but when I make the API call the same string becomes

"0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D"

Here is my code:

  @GET("sendotp")
        Call<ResponseBody> getKey(@QueryMap Map<String,String> map);

  Map<String,String> jsonMap=new HashMap<String,String>();
        

jsonMap.put("par","0wDs7cLFw63nXIQc%2FodiEJr0DWgJJJGj%2ByALAIaN%2F0TnYKp7yDYcKjE8Kftf%2Bg7tA68DMz5g%2FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%3D");

Call<ResponseBody> callSendOTP=RetrofitSingleton.getInstance().get_Api().getKey(jsonMap);

Using interceptor, I got this:

https://http://046ba9a626ae.ngrok.io/webresources/generic/key?par=0wDs7cLFw63nXIQc%252FodiEJr0DWgJJJGj%252ByALAIaN%252F0TnYKp7yDYcKjE8Kftf%252Bg7tA68DMz5g%252FhJAfmKhzOKVESmrw0OUviTiFz1yQI2IQPM%253D 

Solution

  • In Retrofit2 Parameter names and values are URL encoded by default. You need to add encoded = true to change this behaviour.

    void createMyStuff(
         @Header("X-Signature") String authorization,
         @Query(value="nickname", encoded = true) String nickname,
         @Query("language") String language,
         Callback<MyAPIResponse> cb);