Надсилання HTTP-запиту

Rust:
Надсилання HTTP-запиту

How to:

Як це зробити:

use reqwest; // Add to Cargo.toml: reqwest = "0.11"
use std::error::Error;

#[tokio::main] // Uses async main function with Tokio runtime
async fn main() -> Result<(), Box<dyn Error>> {
    let response = reqwest::get("https://api.example.com/data")
        .await?
        .text()
        .await?;

    println!("Response: {}", response);
    Ok(())
}

Sample output:

Response: {"key": "value"}

Deep Dive:

Поглиблений розгляд: HTTP requests have been fundamental to web communication since Tim Berners-Lee’s first browser. Alternatives like gRPC are picking up steam for performance reasons but aren’t as widespread yet. Knowing HTTP is foundational.

Sending an HTTP request in Rust engages external crates like reqwest, which simplifies tasks. Libraries like hyper provide lower-level access and more control. Rust’s async ecosystem, including tokio, makes handling concurrent requests efficient.

See Also:

Дивіться також: