Why is Retrofit missing certain characters from my url request?

Update: The problem is NOT when url encode is required. This is done using Retrofit. If I encode the URL and then pass it to Retrofit, the URL is encoded twice and ends up like this:

https://example.com/api/get_user_info/?id=12345& cookie=user@email.com% 257C1492357107% 257CfyVzRUYC9h% 257C4f889e1976c2cd87aac

Pay attention to %257C

. Round one | -> %7C

. Second round%7C -> %257C

I am trying to make an HTTP GET request using Retrofit. The request looks like this: https://example.com/api/get_user_info/?id=12345& cookie=user@email.com | 1492357107 | fyVzRUYC9h | 4f889e1976c2cd87aac

In the code, the call looks like this:

@GET("get_user_info")
Call<ResponseBody> getUserMeta(
            @Query("id") int userId,
            @Query("cookie") String cookie
);

      

When I execute this request it Request

looks like this:

Request{method=GET, url=https://example.com/api/get_user_info/?id=12345&cookie=user@email.com|1492357107|fyVzRUYC9h|4f889e1976c2cd87aac, tag=null}

      

(Removed from console after registration call.request().toString()

)

After the call is done, I get an error from my API stating that the cookie is not valid. The answer looks like this:

Response{protocol=h2, code=404, message=, url=https://example.com/api/get_user_info/?id=12345&cookie=user@email.com1492357107fyVzRUYC9h4f889e1976c2cd87aac}

      

(Removed from console after loggin Response<ResponseBody> response.toString()

)

As you can see, these two URLs are not the same. The character was |

somehow removed from the cookie parameter in the response url.

You might think that this has something to do with my API and that the cookie is actually incorrect, but if I copy and paste the URL Request

directly into my browser it runs without error (status 200). If Retrofit makes the call, I I get 404.

Obviously Retrofit is executing the second wrong url. This is clearly the case because the first url doesn't return a 404 when executed in the browser, but the second does.

What's happening? Any help would be much appreciated.

Additional Information:

I am using compile 'com.squareup.retrofit2:retrofit:2.2.0'

build.gradle in my app module.

I instantiate my Retrofit client like this:

Retrofit retroFit = new Retrofit.Builder()
                    .baseUrl("https://example.com/api/")
                    .build();

      

+3


source to share


2 answers


You must implement encoding

in your cookies:



@FormUrlEncoded
@GET("get_user_info")
Call<ResponseBody> getUserMeta(
    @Query("id") int userId,
    @Query(value ="cookie", encode = true) String cookie

    );

      

+1


source


Don't pass the feed character directly, encode it to HTML, your url should look like this:

&cookie=user@email.com%7C1492357107%7CfyVzRUYC9h%7C4f889e1976c2cd87aac

      

Pay attention to | character replaced with% 7C



Or you can use the URLEncoder function:

String encodedUrl = URLEncoder.encode("https://example.com/api/get_user_info/?id=12345&cookie=user@email.com|1492357107|fyVzRUYC9h|4f889e1976c2cd87aac", "utf-8");

      

-1


source







All Articles