Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)?

like image 217
Alfie Hanssen Avatar asked Aug 13 '13 20:08

Alfie Hanssen


1 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.

like image 157
frederick_c_siu Avatar answered Sep 25 '22 14:09

frederick_c_siu