18

Is there a way to set a custom cookie on retrofit requests?

Either by using the RequestInterceptor or any other means?

4 Answers 4

33

Through the retrofit.RequestInterceptor:

@Override
public void intercept(RequestFacade request) {    
     request.addHeader("Cookie", "cookiename=cookievalue");
}

You can set a custom RequestInterceptor as follows:

String cookieKey = ...
String cookieValue = ...

RestAdapter adapter = new RestAdapter.Builder()
    .setRequestInterceptor(new RequestInterceptor() {
      @Override
      public void intercept(RequestFacade request) {
        // assuming `cookieKey` and `cookieValue` are not null 
        request.addHeader("Cookie", cookieKey + "=" + cookieValue);
      }
    })
    .setServer("http://...")
    .build();

YourService service = adapter.create(YourService.class);

And to read any cookies set by the server, attach a custom cookie manager like this:

OkHttpClient client = new OkHttpClient();
CustomCookieManager manager = new CustomCookieManager();
client.setCookieHandler(manager);

RestAdapter adapter = new RestAdapter.Builder()
    .setClient(new OkClient(client))
    ...
    .build();

where CustomCookieManager could look like this:

public class CustomCookieManager extends CookieManager {

  // The cookie key we're interested in.    
  private final String SESSION_KEY = "session-key";

  /**
   * Creates a new instance of this cookie manager accepting all cookies.
   */
  public CustomCookieManager() {
    super.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
  }

  @Override
  public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException {

    super.put(uri, responseHeaders);

    if (responseHeaders == null || responseHeaders.get(Constants.SET_COOKIE_KEY) == null) {
      // No cookies in this response, simply return from this method.
      return;
    }

    // Yes, we've found cookies, inspect them for the key we're looking for.
    for (String possibleSessionCookieValues : responseHeaders.get(Constants.SET_COOKIE_KEY)) {

      if (possibleSessionCookieValues != null) {

        for (String possibleSessionCookie : possibleSessionCookieValues.split(";")) {

          if (possibleSessionCookie.startsWith(SESSION_KEY) && possibleSessionCookie.contains("=")) {

            // We can safely get the index 1 of the array: we know it contains
            // a '=' meaning it has at least 2 values after splitting.
            String session = possibleSessionCookie.split("=")[1];

            // store `session` somewhere

            return;
          }
        }
      }
    }
  }
}
6
  • where did you add this code exactly and did you have to do anything else? Also, what goes in cookiename=cookievalue?
    – Lion789
    Mar 27, 2014 at 14:27
  • @Eric: How did you obtain the cookievalue from the response?
    – Chetna
    Mar 28, 2014 at 10:41
  • I want to know the same thing
    – frankelot
    May 27, 2014 at 14:19
  • @David What is Constants.SET_COOKIE_KEY?
    – tread
    May 1, 2015 at 11:59
  • @David How do you add the cookie to sharedPreferences in the CookieManager?
    – tread
    May 1, 2015 at 13:27
3

This is how it's done for retrofit2

Gradle:

compile 'com.squareup.retrofit2:retrofit:2.1.0'

The code:

static final class CookieInterceptor implements Interceptor {
            private volatile String cookie;

            public void setSessionCookie(String cookie) {
                this.cookie = cookie;
            }

            @Override
            public okhttp3.Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                if (this.cookie != null) {
                    request = request.newBuilder()
                            .header("Cookie", this.cookie)
                            .build();
                }
                return chain.proceed(request);
            }
}


class Creator {

    public static MyApi newApi() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .create();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new CookieInterceptor())
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(MyApi.URL)
                .callFactory(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        return retrofit.create(MyApi.class);
    }
}
1

Another way to set a cookie is this way:

@Headers("Cookie: cookiename=cookievalue")
@GET("widget/list")
Call<List<Widget>> widgetList();

And here is a dynamic way:

@GET("user")
Call<User> getUser(@Header("Cookie") String cookie)
0

I've only just started with RetroFit, but the way it handles cookies does not seem to be on par with the rest of the library. I wound up doing something like this:

// Set up system-wide CookieHandler to capture all cookies sent from server.
final CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);

// Set up interceptor to include cookie value in the header.
RequestInterceptor interceptor = new RequestInterceptor() {
  @Override
  public void intercept(RequestFacade request) {
    for (HttpCookie cookie : cookieManager.getCookieStore().getCookies()) {
      // Set up expiration in format desired by cookies
      // (arbitrarily one hour from now).
      Date expiration = new Date(System.currentTimeMillis() + 60 * 60 * 1000);
      String expires = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz")
          .format(expiration);

      String cookieValue = cookie.getName() + "=" + cookie.getValue() + "; " +
          "path=" + cookie.getPath() + "; " +
          "domain=" + cookie.getDomain() + ";" +
          "expires=" + expires;

      request.addHeader("Cookie", cookieValue);
    }
  }
};

RestAdapter restAdapter = new RestAdapter.Builder()
    .setEndpoint("https://api.github.com")
    .setRequestInterceptor(interceptor) // Set the interceptor
    .build();

GitHubService service = restAdapter.create(GitHubService.class);
1
  • I definitely agree with your statement that the cookie handling is sub-par.
    – moswald
    Aug 13, 2014 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.