mirror of
https://github.com/fafhrd91/actix-web
synced 2025-01-18 13:51:50 +01:00
docs: add tls to awc example
This commit is contained in:
parent
34327bd221
commit
ee6a6ec03e
@ -153,9 +153,9 @@ tokio = { version = "1.24.2", features = ["rt-multi-thread", "macros"] }
|
|||||||
zstd = "0.13"
|
zstd = "0.13"
|
||||||
tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests
|
tls-rustls-0_23 = { package = "rustls", version = "0.23" } # add rustls 0.23 with default features to make aws_lc_rs work in tests
|
||||||
|
|
||||||
[lints]
|
|
||||||
workspace = true
|
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "client"
|
name = "client"
|
||||||
required-features = ["rustls-0_23-webpki-roots"]
|
required-features = ["rustls-0_23-webpki-roots"]
|
||||||
|
|
||||||
|
[lints]
|
||||||
|
workspace = true
|
||||||
|
@ -1,25 +1,39 @@
|
|||||||
use std::error::Error as StdError;
|
//! 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;
|
||||||
|
|
||||||
/// If we want to make requests to addresses starting with `https`, we need to enable the rustls feature of awc
|
|
||||||
/// `awc = { version = "3.5.0", features = ["rustls"] }`
|
|
||||||
#[actix_rt::main]
|
#[actix_rt::main]
|
||||||
async fn main() -> Result<(), Box<dyn StdError>> {
|
async fn main() -> Result<(), Box<dyn StdError>> {
|
||||||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
|
|
||||||
// construct request builder
|
let mut config = ClientConfig::builder()
|
||||||
let client = awc::Client::new();
|
.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();
|
||||||
|
|
||||||
// configure request
|
// configure request
|
||||||
let request = client
|
let request = client
|
||||||
.get("https://www.rust-lang.org/")
|
.get("https://www.rust-lang.org/")
|
||||||
.append_header(("User-Agent", "Actix-web"));
|
.append_header(("User-Agent", "awc/3.0"));
|
||||||
|
|
||||||
println!("Request: {:?}", request);
|
println!("Request: {request:?}");
|
||||||
|
|
||||||
let mut response = request.send().await?;
|
let mut response = request.send().await?;
|
||||||
|
|
||||||
// server response head
|
// server response head
|
||||||
println!("Response: {:?}", response);
|
println!("Response: {response:?}");
|
||||||
|
|
||||||
// read response body
|
// read response body
|
||||||
let body = response.body().await?;
|
let body = response.body().await?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user