Retrofit: Too many follow-up requests: 21

Created on 2 Feb 2016  路  10Comments  路  Source: square/retrofit

Hello,

i'm using retrofit to make requests.
I've got following error:

java.net.ProtocolException: Too many follow-up requests: 21

The code is like below:

private OkHttpClient httpClient;
private CookieManager cookieManager;

public <S> S createCookieService(Class<S> serviceClass) {
    httpClient.interceptors().clear();
    httpClient.setCookieHandler(cookieManager);

    Retrofit.Builder builder = new Retrofit
            .Builder()
            .client(httpClient)
            .baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.client(httpClient).build();

    return retrofit.create(serviceClass);
}

And then i'm making requests:

example:

1) login

@POST("/login")
 Call<User> login();

2) some request:

@GET("/request")
Call<PojoPojo> getPojo();

And i'm getting this error too many follow-up requests: 21.

Please help.

Most helpful comment

This gets thrown (by OkHttp, not Retrofit) when there are more than 20 redirects when calling an endpoint. Usually this indicates a redirect cycle between two endpoints. Both Chrome and Firefox will also stop loading the request after this many redirects and fail the request.

You need to consult with your server team or endpoint documentation to ensure you are passing the correct data directly to the endpoint you want to call. No action for Retrofit to take here.

All 10 comments

This gets thrown (by OkHttp, not Retrofit) when there are more than 20 redirects when calling an endpoint. Usually this indicates a redirect cycle between two endpoints. Both Chrome and Firefox will also stop loading the request after this many redirects and fail the request.

You need to consult with your server team or endpoint documentation to ensure you are passing the correct data directly to the endpoint you want to call. No action for Retrofit to take here.

Ok, thanks.

Well, instead of 401 I am getting this error too.

so the server should stop prompting for credentials (in my case basic authentication was wrong) resulting in this behavior.

I only get this error with Proguard enabled. See my thread on stackoverflow:

http://stackoverflow.com/questions/40605124/protocolexception-with-proguard-and-okhttp-3-0

@JakeWharton

We have found a solution when credentiel not valid .
so we can add the solution in the okHttpClient.authenticator . This will stop cached retry.

Here is the solution :
okHttpClient.authenticator(new Authenticator() {
@Override
public Request authenticate(Route route, Response response) throws IOException {

            String credential = Credentials.basic(username, password);

            if (credential.equals(response.request().header("Authorization"))) {
                return null; // If we already failed with these credentials, don't retry.
            }

            return response.request().newBuilder()
                    .header("Authorization", credential)
                    .build();
        }
    });

Why does it not just give me a response with code 401?

I faced the same issue, it got solved by following this approach :https://stackoverflow.com/a/43014982/1271129

After too many hours to solve this, for me the issue was the url was starting with "/"
Replace url @GET("/users") with @GET("user"), and the base url of the api should end with "/".

Well for me the solution worked was to removing the Authenticator class from the RetrofitBuilder

Was this page helpful?
0 / 5 - 0 ratings