### Async Request with Digest Auth using Tuple Credentials Source: https://context7.com/maoertel/diqwest/llms.txt Use the `send_digest_auth()` method with tuple credentials for simple GET or POST requests. This method automatically handles the digest authentication flow, including retrying requests with the correct Authorization header. ```rust use diqwest::WithDigestAuth; use reqwest::Client; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); // Simple GET request with digest auth using tuple credentials let response = client .get("https://httpbin.org/digest-auth/auth/user/passwd") .send_digest_auth(("user", "passwd")) .await?; println!("Status: {}", response.status()); println!("Body: {}", response.text().await?); // POST request with JSON body and digest auth let response = client .post("https://api.example.com/protected/resource") .json(&serde_json::json!({ "key": "value", "data": [1, 2, 3] })) .send_digest_auth(("username", "password")) .await?; Ok(()) } ``` -------------------------------- ### Simple Authentication with Credentials Struct Source: https://context7.com/maoertel/diqwest/llms.txt The `Credentials` struct provides an owned container for one-off requests. It's useful for passing credentials around or storing them temporarily without enabling session caching. Reusable credentials are created once, while new credentials can be created for each request if needed. ```rust use diqwest::{Credentials, WithDigestAuth}; use reqwest::Client; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); // Create reusable credentials (no caching, allocates strings) let creds = Credentials::new("api_user", "secret_key"); // Use credentials for a request let response = client .get("https://api.example.com/protected") .send_digest_auth(creds) .await?; // Create new credentials for another request let other_creds = Credentials::new( std::env::var("API_USER")?, std::env::var("API_PASSWORD")? ); let response = client .delete("https://api.example.com/resource/123") .send_digest_auth(other_creds) .await?; Ok(()) } ``` -------------------------------- ### Configure Blocking Mode Source: https://context7.com/maoertel/diqwest/llms.txt Enable the blocking feature in Cargo.toml to perform synchronous HTTP requests. ```toml # Cargo.toml [dependencies] diqwest = { version = "4", features = ["blocking"] } reqwest = { version = "0.13", features = ["blocking"] } ``` -------------------------------- ### Perform Synchronous Requests Source: https://context7.com/maoertel/diqwest/llms.txt Use the blocking client and DigestAuthSession for synchronous authentication workflows. ```rust use diqwest::blocking::WithDigestAuth; use diqwest::DigestAuthSession; use reqwest::blocking::Client; fn main() -> Result<(), Box> { let client = Client::new(); // Simple request with tuple credentials let response = client .get("https://httpbin.org/digest-auth/auth/user/passwd") .send_digest_auth(("user", "passwd"))?; println!("Status: {}", response.status()); // Using session for cached authentication let session = DigestAuthSession::new("admin", "secret"); let response1 = client .get("https://api.example.com/data") .send_digest_auth(&session)?; // Subsequent requests use cached credentials let response2 = client .post("https://api.example.com/data") .json(&serde_json::json!({"value": 42})) .send_digest_auth(&session)?; Ok(()) } ``` -------------------------------- ### Cached Authentication with DigestAuthSession Source: https://context7.com/maoertel/diqwest/llms.txt The `DigestAuthSession` struct caches credential contexts per host, enabling preemptive authentication on subsequent requests to the same host and improving performance by eliminating the 401 round-trip. A new cache entry is created for each distinct host. ```rust use diqwest::{DigestAuthSession, WithDigestAuth}; use reqwest::Client; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); let session = DigestAuthSession::new("username", "password"); // First request: triggers 401 -> calculates auth -> 200 (caches context) let response1 = client .get("https://api.example.com/users") .send_digest_auth(&session) .await?; println!("First request status: {}", response1.status()); // Second request: uses cached credentials (preemptive auth, no 401) let response2 = client .get("https://api.example.com/users/123") .send_digest_auth(&session) .await?; println!("Second request status: {}", response2.status()); // Third request to same host: also uses cache let response3 = client .post("https://api.example.com/users") .json(&serde_json::json!({"name": "New User"})) .send_digest_auth(&session) .await?; // Request to different host: separate cache entry created let response4 = client .get("https://other-api.example.com/data") .send_digest_auth(&session) .await?; Ok(()) } ``` -------------------------------- ### Send Request with Digest Auth (Credentials Struct) Source: https://github.com/maoertel/diqwest/blob/main/README.md Use this for one-off requests when you prefer to use the `Credentials` struct. Requires `reqwest` and `diqwest` with the `WithDigestAuth` trait. ```rust use diqwest::Credentials; use diqwest::WithDigestAuth; use reqwest::Client; let creds = Credentials::new("username", "password"); let response = Client::new() .get("https://example.com/api") .send_digest_auth(creds) .await?; ``` -------------------------------- ### Handle diqwest Errors Source: https://context7.com/maoertel/diqwest/llms.txt Match against diqwest::error::Error variants to handle specific authentication or network failures. ```rust use diqwest::{WithDigestAuth, DigestAuthSession}; use diqwest::error::Error; use reqwest::Client; #[tokio::main] async fn main() { let client = Client::new(); let session = DigestAuthSession::new("user", "pass"); let result = client .get("https://api.example.com/protected") .send_digest_auth(&session) .await; match result { Ok(response) => { println!("Success: {}", response.status()); } Err(Error::Reqwest(e)) => { // Network or HTTP errors from reqwest eprintln!("HTTP error: {}", e); } Err(Error::DigestAuth(e)) => { // Digest auth calculation errors eprintln!("Auth calculation error: {}", e); } Err(Error::AuthHeaderMissing) => { // Server returned 401 but no www-authenticate header eprintln!("Server requires auth but didn't provide challenge"); } Err(Error::RequestBuilderNotCloneable) => { // Request body is a stream (not supported) eprintln!("Cannot use streaming body with digest auth"); } Err(Error::MissingHost) => { // URL has no host component eprintln!("Invalid URL: missing host"); } Err(Error::LockPoisoned) => { // Internal session lock error (rare) eprintln!("Session lock was poisoned"); } Err(e) => { eprintln!("Other error: {}", e); } } } ``` -------------------------------- ### DigestAuthSession for Multiple Requests Source: https://github.com/maoertel/diqwest/blob/main/README.md Utilize `DigestAuthSession` to cache credentials for multiple requests to the same server, avoiding the 401 challenge on subsequent requests and improving performance. Requires `reqwest` and `diqwest` with `WithDigestAuth` and `DigestAuthSession`. ```rust use diqwest::{WithDigestAuth, DigestAuthSession}; use reqwest::Client; let client = Client::new(); let session = DigestAuthSession::new("username", "password"); // First request: 401 -> auth -> 200 (credentials cached) let resp1 = client.get("https://example.com/api") .send_digest_auth(&session) .await?; // Subsequent requests: preemptive auth -> 200 (no 401 challenge) let resp2 = client.get("https://example.com/other") .send_digest_auth(&session) .await?; ``` -------------------------------- ### Send Request with Digest Auth (Tuple Credentials) Source: https://github.com/maoertel/diqwest/blob/main/README.md Use this for one-off requests where credentials can be passed as a tuple to avoid allocation. Requires `reqwest` and `diqwest` with the `WithDigestAuth` trait. ```rust use diqwest::WithDigestAuth; use reqwest::Client; // Using a tuple (no allocation for credentials) let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password")) .await?; ``` -------------------------------- ### Blocking Request with Digest Auth Source: https://github.com/maoertel/diqwest/blob/main/README.md Send blocking HTTP requests with digest authentication. Ensure the `blocking` feature is enabled in your `Cargo.toml`. Requires `reqwest::blocking` and `diqwest::blocking::WithDigestAuth`. ```rust use diqwest::blocking::WithDigestAuth; use reqwest::blocking::Client; let response = Client::new() .get("https://example.com/api") .send_digest_auth(("username", "password"))?; ``` -------------------------------- ### Manually Invalidate DigestAuthSession Cache Source: https://github.com/maoertel/diqwest/blob/main/README.md Provides methods to manually clear the cached credentials for a specific host or for all hosts within a `DigestAuthSession`. This is useful if you suspect the server's nonce has become stale. ```rust # use diqwest::DigestAuthSession; # let session = DigestAuthSession::new("username", "password"); // Invalidate cache for a specific host session.invalidate_host("example.com")?; // Invalidate all cached credentials session.invalidate_all()?; ``` -------------------------------- ### Invalidate DigestAuthSession Cache Source: https://context7.com/maoertel/diqwest/llms.txt Use invalidate_host or invalidate_all to clear cached authentication credentials when credentials change or re-authentication is required. ```rust use diqwest::{DigestAuthSession, WithDigestAuth}; use reqwest::Client; #[tokio::main] async fn main() -> Result<(), Box> { let client = Client::new(); let session = DigestAuthSession::new("user", "password"); // Make some authenticated requests client .get("https://api.example.com/resource") .send_digest_auth(&session) .await?; client .get("https://other-api.example.com/data") .send_digest_auth(&session) .await?; // Invalidate cache for a specific host (e.g., after password change) session.invalidate_host("api.example.com")?; // Next request to api.example.com will go through 401 challenge again client .get("https://api.example.com/resource") .send_digest_auth(&session) .await?; // Invalidate all cached credentials session.invalidate_all()?; // All subsequent requests will require fresh authentication client .get("https://other-api.example.com/data") .send_digest_auth(&session) .await?; Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.