Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHttp doesn't redirect POST requests when used with retrofit

Using retrofit I want to make POST request to http://milzinas.lt/oauthsilent/authorize. This URL is special because it redirects you to http://milzinas.e-bros.lt/oauthsilent/authorize. My retrofit setup uses OkHttpClient. If I make request using OkHttpClient only then redirecting works fine, i.e. 401 status code is received. However, when I use the same OkHttpClient with retrofit then response is status code 307. I think this has something to do with OkClient implementation which wraps the OkHttpClient but I'm not sure. Below is the code I used to test this scenario. I'm using these libraries:

com.squareup.retrofit:retrofit:1.9.0
com.squareup.okhttp:okhttp:2.2.0

I understand that when URL is redirecting you to another URL the http client has to make two requests. In my case the first request returns 307 (Temporary Redirect) and the second one returns 401 (Unauthorized). However, retrofit always returns response of the first request. Do you know how to make redirecting work properly with retrofit? Maybe I could achieve this by using some other HTTP client? Any suggestions will be appreciated.

So when I execute code below console prints

Retrofit failure. Status: 307
OkHttp. Status: 401

I want it to be

Retrofit failure. Status: 401
OkHttp. Status: 401

public class MainActivity extends AppCompatActivity {

interface Api {

    @POST(URL)
    @Headers("Accept: application/json")
    void test(@Body Object dummy, Callback<Object> callback);

}

static final String BASE_URL = "http://milzinas.lt";
static final String URL = "/oauthsilent/authorize";

final OkHttpClient okHttpClient = new OkHttpClient();
Api api;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    RestAdapter retrofit = new RestAdapter.Builder()
            .setEndpoint(BASE_URL)
            .setClient(new OkClient(okHttpClient))
            .setConverter(new Converter() {
                @Override
                public Object fromBody(TypedInput body, Type type) throws ConversionException {
                    return null;
                }

                @Override
                public TypedOutput toBody(Object object) {
                    return null;
                }
            })
            .build();

    api = retrofit.create(Api.class);

    makeRequestOkHttp();
    makeRequestRetrofit();
}

void makeRequestOkHttp() {
    new AsyncTask<Object, Object, Object>() {
        @Override
        protected Object doInBackground(Object... objects) {
            try {
                Request request = new Request.Builder().url(BASE_URL + URL).build();
                com.squareup.okhttp.Response response = okHttpClient.newCall(request).execute();
                android.util.Log.d("matka", "OkHttp. Status: " + response.code());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            return null;
        }
    }.execute();
}

void makeRequestRetrofit() {
    api.test("", new Callback<Object>() {
        @Override
        public void success(Object o, Response response) {
            android.util.Log.d("matka", "Retrofit success. Status: " + response.getStatus());
        }

        @Override
        public void failure(RetrofitError error) {
            android.util.Log.d("matka", "Retrofit failure. Status: " + error.getResponse().getStatus());
        }
    });
}

}

like image 352
Egis Avatar asked Apr 14 '16 13:04

Egis


1 Answers

The problem persist even in the latest v3.5.0 The only workaround that works is https://github.com/square/okhttp/issues/936#issuecomment-266430151

like image 199
Preetam Avatar answered Nov 15 '22 09:11

Preetam