Basic Authentication
Recipe | Crates | Categories |
---|---|---|
Perform a basic HTTP authentication |
Perform a basic HTTP authentication
Uses reqwest::RequestBuilder::basic_auth
to perform a basic HTTP authentication.
use std::fs::File;
use std::io::copy;
use anyhow::Result;
use tempfile::Builder;
#[tokio::main]
async fn main() -> Result<()> {
let tmp_dir = Builder::new().prefix("example").tempdir()?;
let target = "https://www.rust-lang.org/logos/rust-logo-512x512.png";
let response = reqwest::get(target).await?;
let mut dest = {
let fname = response
.url()
.path_segments()
.and_then(|segments| segments.last())
.and_then(|name| if name.is_empty() { None } else { Some(name) })
.unwrap_or("tmp.bin");
println!("file to download: '{}'", fname);
let fname = tmp_dir.path().join(fname);
println!("will be located under: '{:?}'", fname);
File::create(fname)?
};
let content = response.text().await?;
copy(&mut content.as_bytes(), &mut dest)?;
Ok(())
}
[basic_authentication: expand (P2)](https://github.com/john-cd/rust_howto/issues/224) authentication/basic.rs is noplayground because of network use. rewrite?