Skip to content

Automatic redirection does not trigger the interceptor, causing the cookieJar interceptor to not work properly #1715

@wilinz

Description

@wilinz

Package

dio, cookie_manager

Version

2.1.1

Output of flutter doctor -v

No response

Dart Version

No response

Steps to Reproduce

final dio = Dio();
final dir = await getApplicationSupportDirectory();
final cookieJar = PersistCookieJar(storage: FileStorage(join(dir.path, "cookies")));
dio.interceptors.add(CookieManager(cookieJar));
dio.get("http://site1") ; // This is a 302 redirect url. The redirected site2 url will return the set-cookie response header

Expected Result

The cookie will be saved to site2

Actual Result

The cookie is saved to the original url, ie site1, resulting in no cookie being carried in subsequent requests.
So I checked the source code cookie_mgr.dart, Should response.requestOptions.uri be changed to response.realUri?

  Future<void> _saveCookies(Response response) async {
    final setCookies = response.headers[HttpHeaders.setCookieHeader];

    if (setCookies != null) {
      final cookies = setCookies
          .map((str) => str.split(_setCookieReg))
          .expand((element) => element);
      await cookieJar.saveFromResponse(
        response.requestOptions.uri, // Should this be changed to response.realUri ?
        cookies.map((str) => Cookie.fromSetCookieValue(str)).toList(),
      );
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions