make a http head request in rust

To make an HTTP HEAD request in Rust, you can use the reqwest crate. Here's an example:

main.rs
use reqwest::Client;

fn main() -> Result<(), reqwest::Error> {
    let client = Client::new();
    let response = client.head("http://example.com").send()?;
    let headers = response.headers();
    println!("{:#?}", headers);
    Ok(())
}
241 chars
10 lines

First, we create a new Client instance using reqwest::Client::new(). Then, we use the head method on the Client instance to send an HTTP HEAD request to the specified URL. The send method returns a Result<reqwest::Response, reqwest::Error>. We can then access the response headers using the headers method on the Response instance.

Note that this code will print the headers to the console. You can modify the code to suit your needs, such as parsing the headers or using them in some other way.

related categories

gistlibby LogSnag