Is there a way to show more cookies fields from webview_flutter's WebViewCookieManager


I am currently playing a bit with the webview_flutter package, especially with the WebViewCookieManager object. While collecting some cookies from different website, it seems only a few part of the cookie is collected (its name, value, domain and path) but nothing about expiration for example. Based on cURL cookie overview, 7 fields should be present in a cookie, here, only 4 are available. If I correctly understand the code, the Dart CookieManager for Android is an interface to the Java CookieManager class, and the cookies should be returned as String via the getCookie method.

This value seems to be parsed by the getCookies method from webview_flutter_android package. Here the code:

final webViewCookies = <WebViewCookie>[]; for (final String cookie in cookies.split('; ')) { final List<String> cookieValue = cookie.split('='); webViewCookies.add( WebViewCookie(name: cookieValue.first, value: cookieValue.last, domain: url.toString()), ); }

Based on this snippet, I assume only the first part of the cookie is parsed and used by this package. Now, my question before opening an issue or a pull request, is there a - clean or easy - way to parse the whole cookie? or just retrieve it as a String or Map?

I wanted to create my own module locally to overwrite this one during compilation, but it's kinda hard. Another idea is to create my own AndroidWebViewCookieManagerCreationParams extending the PlatformWebViewCookieManagerCreationParams class, but it seems also quite complex (and risky, even more if I need to use it also for iOS).

-2
Oct 13 at 3:53 PM
User AvatarMathieu Kerjouan
#android#flutter#dart#cookies#webview-flutter

Accepted Answer

Not really, and the limit isn’t in the Flutter package. On Android, CookieManager.getCookie(url) only returns what a browser would put in the Cookie header, so it’s just name=value; name2=value2. Expiry, path, Secure and HttpOnly are never exposed, so webview_flutter_android has nothing else to parse (and domain is just the URL you passed in).

If you need the full attributes, you’d have to read the Set-Cookie headers yourself, e.g. by making the request with dio or http. flutter_inappwebview has a richer cookie model, but Android support for those fields is limited for the same reason. iOS does expose full cookies through WKHTTPCookieStore, but webview_flutter only maps four fields.

Side note: that split('=') with .last will mangle any cookie value containing = (base64, for example), so that part is worth an issue.

User AvatarMahendra Dhakad
Oct 14 at 7:12 AM
4