Confuse on construct cookies and pass them into reqwest::Client

I'm trying convert a .NETFramework library about Steam to Rust library, as my first Rust project for further use.

It requires HTTP client so I choose reqwest = { version = "0.11.12", features = ["cookies"] } since this is the only library I know(If there exist a more suitable library I'd appreciate switch to that one).

I should mention that, in my poor HTTP related knowledge, cookies is a separate data storage that not relate with header, I don't have a clear process on how cookies are transfer during full process, and haven't deal with any pipe, session relate data knowledge. Also I haven't write any big enough project in any language to satisfy I'm ready to dealing realistic problem.
So if you find some describe below wrong, then probably I'm wrong.

When I rewrite to a cookie container, It attach a set of cookies to a session, and I search reqwest about cookie, got reqwest::cookie::Jar which suit for CookieContainer and reqwest::cookie::Cookie which suit for Cookie, but:

  1. reqwest::cookie::Cookie does't have it's new() method, I have to use reqwest::cookie::Jar::add_cookie_str() to construct a cookie, so I though I'm wrong;
  2. reqwest::cookie::Jar::add_cookie_str() didn't include a parameter named path, although in original library the value of path is root("/") so I though no problem, but I'd assume I'm on a wrong way;
  3. reqwest::cookie::Jar and reqwest::cookie::Cookie doesn't have any relation with reqwest::Client, the only search result outer that reqwest::cookie -- reqwest::header::COOKIE is a constant.

So I assume I'm totally wrong.

Then I search in reqwest_cookie_store crate as reqwest said. But still can't find a clear way from construct a cookie to pass-in to client.

I remember yesterday I finally found that cookie crate is what they use, but still not going well, so at this new day I decide come here to find help to prevent more time waste by my poor knowledge.

You don't need to do any of this.
In reqwest, you construct a Client using ClientBuilder.

You use the cookie_provider method on the builder to enable cookie storage.
This method requires you pass an Arc<C> where C implements the CookieJar trait. The Jar struct implements this trait. You can use that. This is useful when you're setting cookies directly.

Alternatively, you can use the cookie_store method on the builder to enable the Client to persist cookies set by the server. The cookies are persisted as long as you use the same Client.

First, thank for your reply, I won't find this method without you point out.

Haven't try it, seem this will solve "pass a set of cookies to client".

But what about construct a cookies, I didn't find a way to construct and modify(set HttpOnly, and Secure property to true), and more even there isn't a method in Jar that add or extract (to modify) cookie. Or maybe it pass out a value and modify that value will also modify cookie?

Generally speaking a client application shouldn't need to manually control those properties, they should be passed in a header from the server. As a client your job is generally to follow the instructions the server sends for how to store a cookie value, and then send the cookie value with subsequent requests (when it's applicable).

Oh, I can understand if you say that, I forgot Rust generally facing high speed and not in common use case even out of my imagine.

But is there a library that suitable for this use case?

I'm not saying you can't, my point is that I'm not really sure what you're trying to do here.

If you want to supply some sort of dynamic custom cookie, you need to implement CookieStore and set your implementation on the ClientBuilder, but I'm not sure that's actually what you need to do for your use case.

You need to import the CookieJar trait to access those methods.

// Create a cookie jar we can share with the HTTP client
let jar = Arc::new(Jar::default());

// create the HTTP client
let client = Client::builder()
    .cookie_provider(Arc::clone(&jar))
    .cookie_store(true)
    .build()
    .unwrap();

// Add some cookies
let cookie = HeaderValue::from_static(
    "password=my_super_secret_password; Expires=Thu, 21 Oct 2021 07:28:00 GMT; Secure; HttpOnly",
);
let url = "https://example.com".parse().unwrap();
jar.set_cookies(&mut [cookie].iter(), &url);

(playground)

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.