Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit - Too many follow-up requests: 21

Tags:

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.

like image 338
Marcin Bortel Avatar asked Feb 01 '16 13:02

Marcin Bortel


People also ask

What does too many follow up requests 21 mean?

Typically this happens when you get into a redirect loop, or when you authenticate with the wrong username, and then retry with the same wrong username. You can fix your Authenticator to check if it is a follow-up request and not retry in that case.

What is an interceptor in retrofit?

Interceptors, according to the documentation, are a powerful mechanism that can monitor, rewrite, and retry the API call. So, when we make an API call, we can either monitor it or perform some tasks. In a nutshell, Interceptors function similarly to airport security personnel during the security check process.


2 Answers

Jake Wharton wrote:

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.

And rest of the thread is there: https://github.com/square/retrofit/issues/1561

like image 109
Marcin Bortel Avatar answered Sep 28 '22 00:09

Marcin Bortel


For me the issue was: the request url was starting with "/".
Replace url @GET("/request") with @GET("request")

  • also the base url of the api should end with "/"
  • if using an Authorization header check if you need to set the value as "Bearer " + token instead

using retrofit 2.4.0 version:

<dependency>     <groupId>com.squareup.retrofit2</groupId>     <artifactId>retrofit</artifactId>     <version>2.4.0</version> </dependency> 
like image 21
Mihai Morcov Avatar answered Sep 27 '22 22:09

Mihai Morcov