getStoredCookies can behave unexpectedly
0xpr03 opened this issue · 0 comments
Describe the bug
Requests.getStoredCookies
has no clear definition what to pass as URL if you want to retrieve the cookie returned from a request you just made.
To Reproduce
Steps to reproduce the behavior:
final url = "http://127.0.0.1:4563/api/login";
final loginResult = await Requests.post(url,
body: {"email": _username, "password": _password},
bodyEncoding: RequestBodyEncoding.JSON,
persistCookies: true);
CookieJar cookies = await Requests.getStoredCookies(url);
// print!(cookies); inspect cookies somehow..
Will return nothing if the cookie returned from this request is defined as
"set-cookie" -> "API_SESSION=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX; HttpOnly; SameSite=Lax; Path=/api; Max-Age=25199"
What you have to do, after reading the code:
//..[post]
var uri = Uri.parse(url);
CookieJar cookies = await Requests.getStoredCookies(uri.host);
But this is brittle. The main branch already changed for that part, so the actual key I have to use for getStoredCookies is not actually stable.
Hostname makes sense at first, but if the cookie is restricted to a Path, like in this case, it gets confusing fast.
Additionally it might be nice to instead provide a way to retrieve cookies obtained by the request directly.
This whole procedure is required if you want to extract cookies for later use.
Expected behavior
Cookie is returned.
Desktop (please complete the following information):
- OS: W10
- requests 4.7.0
- flutter 3.16.3