Rust:
Sending an HTTP request with basic authentication
How to:
First, add the necessary crate to your Cargo.toml
:
[dependencies]
reqwest = "0.11"
base64 = "0.13"
Now, here’s the Rust code to send a GET request with basic authentication:
use reqwest::header::{Authorization, Basic};
use std::error::Error;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = reqwest::Client::new();
let user = "Aladdin";
let password = "open sesame";
let auth = Basic {
username: user.into(),
password: Some(password.into()),
};
let response = client
.get("http://example.com/secrets")
.header(Authorization(auth))
.send()
.await?;
let content = response.text().await?;
println!("Response: {}", content);
Ok(())
}
If correct, it’ll print the secrets. You get the gist.
Deep Dive
Before reqwest
, you’d see folks wrestle with curl
in Rust. It’s like preferring a handsaw over a chainsaw. Basic auth, while easy-peasy, is not Fort Knox. It’s just Base64 of “username:password” – no encryption, so HTTPS is a must.
Alternatives? OAuth 2.0 dances circles around Basic, offering tokens instead of tangible credentials. Still, it’s complex. Then there’s Bearer authentication, holding tokens like a secret handshake.
Under the hood, reqwest
is a high-level HTTP client playing nice with Rust’s async features. The ‘Basic’ struct creates the header, ‘Authorization’ pops it in, and presto, you’re knocking on the server’s door with a secret whisper.
See Also
For more lore and wizardry:
- reqwest documentation: https://docs.rs/reqwest
- Understanding HTTP Basic Access Authentication: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
- Rust async programming: https://rust-lang.github.io/async-book/
- rust base64 crate documentation: https://docs.rs/base64