I need to add cookies with retrofit 2.0. If i understand correct, cookies - the same as headers. this cookies must be added:
private HashMap
why not using the cookieJar option?
new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(
new OkHttpClient().newBuilder()
.cookieJar(new SessionCookieJar()).build())
.build();
private static class SessionCookieJar implements CookieJar {
private List cookies;
@Override
public void saveFromResponse(HttpUrl url, List cookies) {
if (url.encodedPath().endsWith("login")) {
this.cookies = new ArrayList<>(cookies);
}
}
@Override
public List loadForRequest(HttpUrl url) {
if (!url.encodedPath().endsWith("login") && cookies != null) {
return cookies;
}
return Collections.emptyList();
}
}