Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid URL encoding in Retrofit?

I'm using retrofit for my network layer I'm facing a strange issue I've the parameter and it's value look like this

store://0TxIGQMQbObzvU4Apia0V0&callback=

and after encoding it changes to this

store://0TxIGQMQbObzvU4Apia0V0%26callback%3D

and for some reason server is not liking this encoding and I'm getting "HTTP 400 Bad Request".

If I hit without encoding it runs fine so I was wondering is there is any way i can disable the endoing.

I've tried other ways like this

@Query(value "env" encoded = false) String env

but no luck.

This is my network interface class

public interface NetworkService
{
    @GET("v1/public/yql")
    Observable<SeriesEntity> getOnGoingSeries(@Query("q") String query,
                                              @Query("format") String format,
                                              @Query("diagnostics") boolean flag,
                                              @Query("env") String env);


    class Factory {
        public  static NetworkService create()
        {
            OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            clientBuilder.addInterceptor(loggingInterceptor);

            Gson gson = new GsonBuilder().disableHtmlEscaping().create();

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("https://query.yahooapis.com")
                    .client(clientBuilder.build())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();

            return retrofit.create(NetworkService.class);
        }
    }
}

Edit :

I also notice that the issue is only the encoded value of '&' because if i replace it value (whic is %26) it works fine

like image 828
user2934930 Avatar asked Feb 25 '17 21:02

user2934930


People also ask

How do I stop URL encoding?

You can decode the url using javascript Function: decodeURIComponent(Url ); Because Browser encodes the Url for special characters . For example : https://www.example.com is encoded to %20https%3A%2F%2Fwww.example.com. Here the special characters are replaced by % and its ASCI value.

What does %20 replace in URL?

URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

Is URL encoding necessary?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

What is form Urlencoded in retrofit?

Annotation Type FormUrlEncodedDenotes that the request body will use form URL encoding. Fields should be declared as parameters and annotated with @Field . Requests made with this annotation will have application/x-www-form-urlencoded MIME type.


2 Answers

Please Use encoded = true

@Query(value = "email", encoded = true)

@HTTP(method = "DELETE", path = ApiConstant.DELETE_CONTACT_INFO, hasBody = true)

Observable deleteContactInfo( @Query(value = "email", encoded = true) String email);

like image 187
Francis Avatar answered Sep 29 '22 22:09

Francis


the issue I believe is that you're including callback param as part of env one. Try adding @Query("callback") boolean callback to your retrofit interface (and using just store://0TxIGQMQbObzvU4Apia0V0 for env)

like image 33
John O'Reilly Avatar answered Sep 29 '22 21:09

John O'Reilly