-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathrequest_with_cookie_store.rs
50 lines (39 loc) · 1.18 KB
/
request_with_cookie_store.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::sync::Arc;
use rquest::{
cookie::{CookieStore, Jar},
redirect::Policy,
Impersonate,
};
use url::Url;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
let url = Url::parse("https://google.com/")?;
// Build a client to impersonate Safari18
let client = rquest::Client::builder()
.impersonate(Impersonate::Safari18)
.build()?;
// Create a cookie store
// Used to store cookies for specific multiple requests without using the client's cookie store
let jar = Arc::new(Jar::default());
// Make a request
let _ = client
.get(&url)
.redirect(Policy::default())
.cookie_store(jar.clone())
.send()
.await?;
// Print cookies
let cookies = jar.cookies(&url);
log::info!("{:?}", cookies);
// Add a cookie
jar.add_cookie_str("foo=bar; Domain=google.com", &url);
// Make a request
let _ = client.get(&url).cookie_store(jar.clone()).send().await?;
// Print cookies
let cookies = jar.cookies(&url);
log::info!("{:?}", cookies);
Ok(())
}