Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flurl CookieJar backup and restore #758

Closed
Pureaznangel opened this issue Jul 31, 2023 · 6 comments
Closed

Flurl CookieJar backup and restore #758

Pureaznangel opened this issue Jul 31, 2023 · 6 comments

Comments

@Pureaznangel
Copy link

Please describe the feature/enhancement in as much detail as possible.

Hello Tmenier and Everyone,

Is there any way or method that support me to export (backup) and import (restore) CookieJar for later use?

Thanks,

@tmenier
Copy link
Owner

tmenier commented Aug 1, 2023

You could serialize the data manually however you choose and re-hydrate a CookieJar using AddOrReplace. But I would concede that this would be easier if not for the FlurlCookie constructor and read-only properties that make it impossible to, for example, just JSON-serialize/deserialize the whole thing in one shot. I'll keep this open as a possible 4.0 enhancement.

@tmenier tmenier added this to Backlog in Default Project via automation Aug 1, 2023
@tmenier tmenier added the 4.0 label Aug 1, 2023
@lisongkun
Copy link

return Cookie.Count == 0 ? string.Empty : string.Join("; ", Cookie.Select(cookie => $"{cookie.Name}={cookie.Value}"));

@tmenier tmenier moved this from Backlog to 4.0 in Default Project Sep 11, 2023
@tmenier tmenier added breaking and removed breaking labels Sep 12, 2023
@tmenier
Copy link
Owner

tmenier commented Sep 19, 2023

Good news! CookieJar persistence will land in 4.0 as a first-class feature. Here's how it'll work:

// string-based usage pattern
var saved = jar.ToString();
var jar2 = CookieJar.LoadFromString(saved);

// TextWriter-based usage pattern (i.e. save to a file)
using var writer = new StreamWriter("path/to/file");
jar.WriteTo(writer);

using var reader = new StreamReader("path/to/file");
var jar2 = CookieJar.LoadFrom(reader);

For both usage patterns, the text-based representation is simple and human-readable. It contains 3 lines for each cookie, representing the 3 pieces of information needed to fully hydrate a FlurlCookie: the date it was received, the URL from which it originated, and all details received in the Set-Cookie header.

2023-09-19T20:30:08
https://mydomain.com/endpoint
x=foo; Domain=mydomain.com; Secure; HttpOnly
...

BREAKING: A few public utility methods on CookieCutter have changed, though calling these methods directly from application code is not likely very common.

@pinkplus
Copy link

pinkplus commented Nov 5, 2023

@tmenier Thanks for adding the new persistence methods. This is much better than before.

I wonder if it's possible to make use of it with the CookieSession class as well. Now CookieSession initializes its own CookieJar instance. On the other hand, the static LoadFrom method on CookieJar always creates a new instance. As a result, loaded cookie jars can't be used in CookieSession.

What about making the LoadFrom method an instance method instead of a static method? The cookies in the input will override the current values in the jar.

@tmenier
Copy link
Owner

tmenier commented Nov 5, 2023

@pinkplus Thanks for pointing this out, I think I overlooked CookieSession when I did this enhancement. Could you open a new issue though? I 'd like to track it separately in case it doesn't make it out in the initial 4.0 release. Thanks.

@pinkplus
Copy link

pinkplus commented Nov 6, 2023

Sure, created #775.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Default Project
  
Released
Development

No branches or pull requests

4 participants