1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-01-31 02:52:53 +01:00
actix-web/awc/examples/client.rs

44 lines
1.2 KiB
Rust
Raw Normal View History

2024-12-29 15:17:18 +00:00
//! Demonstrates construction and usage of a TLS-capable HTTP client.
extern crate tls_rustls_0_23 as rustls;
use std::{error::Error as StdError, sync::Arc};
use actix_tls::connect::rustls_0_23::webpki_roots_cert_store;
use rustls::ClientConfig;
2019-01-28 20:41:09 -08:00
#[actix_rt::main]
2021-06-17 17:57:58 +01:00
async fn main() -> Result<(), Box<dyn StdError>> {
2022-01-19 21:36:14 +00:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2019-01-28 20:41:09 -08:00
2024-12-29 15:17:18 +00:00
let mut config = ClientConfig::builder()
.with_root_certificates(webpki_roots_cert_store())
.with_no_client_auth();
let protos = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
config.alpn_protocols = protos;
// construct request builder with TLS support
let client = awc::Client::builder()
.connector(awc::Connector::new().rustls_0_23(Arc::new(config)))
.finish();
2019-11-20 23:33:22 +06:00
2022-01-19 21:36:14 +00:00
// configure request
2021-06-19 20:23:06 +01:00
let request = client
2019-11-26 17:16:33 +06:00
.get("https://www.rust-lang.org/")
2024-12-29 15:17:18 +00:00
.append_header(("User-Agent", "awc/3.0"));
2021-06-19 20:23:06 +01:00
2024-12-29 15:17:18 +00:00
println!("Request: {request:?}");
2021-06-19 20:23:06 +01:00
let mut response = request.send().await?;
2019-11-20 23:33:22 +06:00
2022-01-19 21:36:14 +00:00
// server response head
2024-12-29 15:17:18 +00:00
println!("Response: {response:?}");
2019-11-20 23:33:22 +06:00
2019-11-26 17:16:33 +06:00
// read response body
let body = response.body().await?;
println!("Downloaded: {:?} bytes", body.len());
2019-01-28 20:41:09 -08:00
2019-11-26 17:16:33 +06:00
Ok(())
2019-01-28 20:41:09 -08:00
}