jossef / jossef/requests

getStoredCookies can behave unexpectedly

Open
#98 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
143
Forks
52
PR merge metrics
No merged PRs in 30d

Description

**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:

```dart
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](https://github.com/jossef/requests/blob/f8f1b920de47cfbf9d9769033cae4a1862ea2d3f/lib/src/requests.dart#L307):
```dart
//..[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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.