Home » Android » java – Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?

java – Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?

Posted by: admin June 16, 2020 Leave a comment

Questions:

I’m using Square’s Retrofit Client to make simple requests from an Android App. Like so:

  RestAdapter restAdapter = new RestAdapter.Builder()
            .setServer(Configurations.getInstance().plistMap.get("PTBaseURL"))
            .setRequestHeaders(new RequestHeaders() {
                @Override
                public List<Header> get() {
                    List<Header> headers = new ArrayList<Header>();
                        Header authHeader = new Header("Authorization", authType + " " + UserManager.getInstance().currentUser.token);
                        headers.add(authHeader);
                    }
                    return headers;
                }
            })
            .build();

    this.service = restAdapter.create(ClientInterface.class);

One endpoint redirects to a different URL (s3). For an reason not important to this question the redirect request is failing and thus my callback.failure(error) method is being called. I need to be able to access and modify the redirect URL or request at some point, preferable in callback.failure(). How can I do this?

Alternatively, is there a way to set something like followRedirects = false (and intercept the redirect in this way)?

How to&Answers:

With OkHttp:

 public static void setFollowRedirects (boolean auto)
 public OkHttpClient setFollowProtocolRedirects(boolean followProtocolRedirects)

With HttpURLConnection:

public static void setFollowRedirects (boolean auto)
public void setInstanceFollowRedirects (boolean followRedirects)

See discussion here.