Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent Retrofit from automatically following a 302

I have an authentication call that i'm trying to make using Retrofit on Android. The call returns a 302 to either a success or failure page. The original 302 response brings back a session cookie needed to maintain authentication on success, however Retrofit is automatically handing the request off to the redirect url before I get a chance to consume the cookie.

Is there a way to prevent following the redirect? Or is there a way to write a response handler on Retrofit that can add the appropriate header before making the second call?

like image 916
nak5ive Avatar asked Jun 09 '14 19:06

nak5ive


1 Answers

to prevent the redirect you have to configure your client, e.g with OkHttp 2:

private sendRequest() {
    OkHttpClient client = new OkHttpClient();
    client.setFollowRedirects(false);

    connectAdapter = new RestAdapter.Builder()
            .setClient(new OkClient(client))
            .setEndpoint("http://yourendpoint")
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .build();

    connectAdapter.create(YourRequest.class).sendMyRequest("login","password");

}

With OKHTTP 3 (you can read this answer from @gropapa):

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.followRedirects(false);
OkHttpClient httpClient = builder.build();
like image 66
Climbatize Avatar answered Oct 18 '22 02:10

Climbatize