Skip to content

Commit

Permalink
chore: Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Jan 25, 2025
1 parent 65f9b0f commit 5d71a54
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 105 deletions.
2 changes: 2 additions & 0 deletions examples/base_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));

// Build a client to impersonate Edge131
let mut client = Client::builder()
.impersonate(Impersonate::Edge131)
Expand Down
1 change: 0 additions & 1 deletion examples/client_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn main() -> Result<(), rquest::Error> {
.base_url("https://tls.peet.ws");

let text = client.get("/api/all").send().await?.text().await?;

println!("{}", text);

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions examples/headers_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const HEADER_ORDER: &[HeaderName] = &[

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));

// Build a client to impersonate Chrome131
let client = rquest::Client::builder()
.impersonate(Impersonate::Chrome131)
Expand Down
1 change: 1 addition & 0 deletions examples/hickory_dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ async fn main() -> Result<(), rquest::Error> {
.await?
.text()
.await?;

println!("{}", text);

Ok(())
Expand Down
12 changes: 10 additions & 2 deletions examples/impersonate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ async fn main() -> Result<(), rquest::Error> {
.impersonate(Impersonate::Firefox128)
.build()?;

let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);
// Use the API you're already familiar with
let text = client
.get("https://tls.peet.ws/api/all")
.send()
.await?
.text()
.await?;

println!("{}", text);

Ok(())
}
6 changes: 5 additions & 1 deletion examples/impersonate_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use rquest::{Client, Impersonate, ImpersonateOS};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Firefox128
let impersonate = Impersonate::builder()
.impersonate(Impersonate::Firefox128)
Expand All @@ -13,15 +15,17 @@ async fn main() -> Result<(), rquest::Error> {
// Apply the impersonate to the client
let client = Client::builder()
.impersonate(impersonate)
.danger_accept_invalid_certs(true)
.http2_only()
.build()?;

// Use the API you're already familiar with
let text = client
.get("https://tls.peet.ws/api/all")
.send()
.await?
.text()
.await?;

println!("{}", text);

Ok(())
Expand Down
4 changes: 3 additions & 1 deletion examples/impersonate_psk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use rquest::Impersonate;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Firefox133
let client = rquest::Client::builder()
.impersonate(Impersonate::Firefox133)
Expand All @@ -18,6 +19,7 @@ async fn main() -> Result<(), rquest::Error> {
.await?
.text()
.await?;

println!("{}", text);

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions examples/impersonate_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const HEADER_ORDER: &[HeaderName] = &[

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// TLS settings
let tls = TlsSettings::builder()
.curves(CURVES)
Expand Down
10 changes: 7 additions & 3 deletions examples/request_with_cookie_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,26 @@ use url::Url;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

let url = Url::parse("https://google.com/")?;

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
.impersonate(Impersonate::Safari18)
.redirect(Policy::default())
.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).cookie_store(jar.clone()).send().await?;
let _ = client
.get(&url)
.redirect(Policy::default())
.cookie_store(jar.clone())
.send()
.await?;

// Print cookies
let cookies = jar.cookies(&url);
Expand Down
3 changes: 2 additions & 1 deletion examples/request_with_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Firefox128
let client = Client::builder()
Expand All @@ -16,6 +16,7 @@ async fn main() -> Result<(), rquest::Error> {
.await?
.text()
.await?;

println!("{}", text);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/request_with_local_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::IpAddr;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand Down
1 change: 1 addition & 0 deletions examples/request_with_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async fn main() -> Result<(), rquest::Error> {
.await?
.text()
.await?;

println!("{}", resp);

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion examples/request_with_redirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rquest::{redirect::Policy, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand All @@ -16,6 +16,7 @@ async fn main() -> Result<(), rquest::Error> {
.await?
.text()
.await?;

println!("{}", resp);

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/request_with_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rquest::{redirect::Policy, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));
env_logger::init_from_env(env_logger::Env::default().default_filter_or("trace"));

// Build a client to impersonate Safari18
let client = rquest::Client::builder()
Expand Down
2 changes: 2 additions & 0 deletions examples/set_cookie_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));

// Build a client to impersonate Chrome131
let mut client = rquest::Client::builder()
.impersonate(Impersonate::Chrome131)
Expand Down
2 changes: 2 additions & 0 deletions examples/set_cookies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use rquest::Impersonate;

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
env_logger::init_from_env(env_logger::Env::default().default_filter_or("debug"));

// Build a client to impersonate Chrome131
let client = rquest::Client::builder()
.impersonate(Impersonate::Chrome131)
Expand Down
2 changes: 2 additions & 0 deletions examples/set_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ async fn main() -> Result<(), rquest::Error> {
.as_mut()
.headers()
.insert(header::ACCEPT, HeaderValue::from_static("application/json"));

// Use the API you're already familiar with
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);

Expand Down
19 changes: 2 additions & 17 deletions examples/set_impersonate.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
use http::{header, HeaderName};
use rquest::Impersonate;

static HEADER_ORDER: [HeaderName; 6] = [
header::ACCEPT_LANGUAGE,
header::USER_AGENT,
header::ACCEPT_ENCODING,
header::HOST,
header::COOKIE,
HeaderName::from_static("priority"),
];
use rquest::{Client, Impersonate};

#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome131
let mut client = rquest::Client::builder()
let mut client = Client::builder()
.impersonate(Impersonate::Chrome131)
.build()?;

// Set the headers order
client.as_mut().headers_order(&HEADER_ORDER);
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
println!("{}", resp.text().await?);

// Change the impersonate to Safari18
client.as_mut().impersonate(Impersonate::Safari18);
let resp = client.get("https://tls.peet.ws/api/all").send().await?;
Expand Down
58 changes: 24 additions & 34 deletions examples/set_interface.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,41 @@
use rquest::{Client, Impersonate};

#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
))]
#[tokio::main]
async fn main() -> Result<(), rquest::Error> {
// Build a client to impersonate Chrome130
#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
))]
let mut client = Client::builder()
.impersonate(Impersonate::Chrome130)
.interface("eth0")
.build()?;

#[cfg(not(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
)))]
let client = Client::builder()
.impersonate(Impersonate::Chrome126)
.build()?;

#[cfg(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
))]
// Set the interface to eth1
client.as_mut().interface("eth1");

// Use the API you're already familiar with
let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

Ok(())
}

#[cfg(not(any(
target_os = "android",
target_os = "fuchsia",
target_os = "linux",
target_os = "ios",
target_os = "visionos",
target_os = "macos",
target_os = "tvos",
target_os = "watchos"
)))]
fn main() {}
3 changes: 3 additions & 0 deletions examples/set_local_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ async fn main() -> Result<(), rquest::Error> {
.impersonate(Impersonate::Chrome130)
.build()?;

// Use the API you're already familiar with
let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

// Set the local address to `172.200.10.2`
client
.as_mut()
.local_address(IpAddr::from([172, 200, 10, 2]));

// Use the API you're already familiar with
let resp = client.get("https://api.ip.sb/ip").send().await?;
println!("{}", resp.text().await?);

Expand Down
Loading

0 comments on commit 5d71a54

Please sign in to comment.