Skip to main content

awc/
lib.rs

1//! `awc` is an asynchronous HTTP and WebSocket client library.
2//!
3//! # `GET` Requests
4//! ```no_run
5//! # #[actix_rt::main]
6//! # async fn main() -> Result<(), awc::error::SendRequestError> {
7//! // create client
8//! let mut client = awc::Client::default();
9//!
10//! // construct request
11//! let req = client.get("http://www.rust-lang.org")
12//!     .insert_header(("User-Agent", "awc/3.0"));
13//!
14//! // send request and await response
15//! let res = req.send().await?;
16//! println!("Response: {:?}", res);
17//! # Ok(())
18//! # }
19//! ```
20//!
21//! # `POST` Requests
22//! ## Raw Body
23//! ```no_run
24//! # #[actix_rt::main]
25//! # async fn main() -> Result<(), awc::error::SendRequestError> {
26//! let mut client = awc::Client::default();
27//! let response = client.post("http://httpbin.org/post")
28//!     .send_body("Raw body contents")
29//!     .await?;
30//! # Ok(())
31//! # }
32//! ```
33//!
34//! ## JSON
35//! ```no_run
36//! # #[actix_rt::main]
37//! # async fn main() -> Result<(), awc::error::SendRequestError> {
38//! let request = serde_json::json!({
39//!     "lang": "rust",
40//!     "body": "json"
41//! });
42//!
43//! let mut client = awc::Client::default();
44//! let response = client.post("http://httpbin.org/post")
45//!     .send_json(&request)
46//!     .await?;
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! ## URL Encoded Form
52//! ```no_run
53//! # #[actix_rt::main]
54//! # async fn main() -> Result<(), awc::error::SendRequestError> {
55//! let params = [("foo", "bar"), ("baz", "quux")];
56//!
57//! let mut client = awc::Client::default();
58//! let response = client.post("http://httpbin.org/post")
59//!     .send_form(&params)
60//!     .await?;
61//! # Ok(())
62//! # }
63//! ```
64//!
65//! # Response Compression
66//! All [official][iana-encodings] and common content encoding codecs are supported, optionally.
67//!
68//! The `Accept-Encoding` header will automatically be populated with enabled codecs and added to
69//! outgoing requests, allowing servers to select their `Content-Encoding` accordingly.
70//!
71//! Feature flags enable these codecs according to the table below. By default, all `compress-*`
72//! features are enabled.
73//!
74//! | Feature           | Codecs        |
75//! | ----------------- | ------------- |
76//! | `compress-brotli` | brotli        |
77//! | `compress-gzip`   | gzip, deflate |
78//! | `compress-zstd`   | zstd          |
79//!
80//! [iana-encodings]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml#content-coding
81//!
82//! # WebSockets
83//! ```no_run
84//! # #[actix_rt::main]
85//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
86//! use futures_util::{SinkExt as _, StreamExt as _};
87//!
88//! let (_resp, mut connection) = awc::Client::new()
89//!     .ws("ws://echo.websocket.org")
90//!     .connect()
91//!     .await?;
92//!
93//! connection
94//!     .send(awc::ws::Message::Text("Echo".into()))
95//!     .await?;
96//!
97//! let response = connection.next().await.unwrap()?;
98//! assert_eq!(response, awc::ws::Frame::Text("Echo".into()));
99//! # Ok(())
100//! # }
101//! ```
102
103#![allow(unknown_lints)] // temp: #[allow(non_local_definitions)]
104#![allow(
105    clippy::type_complexity,
106    clippy::borrow_interior_mutable_const,
107    clippy::needless_doctest_main
108)]
109#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
110#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
111#![cfg_attr(docsrs, feature(doc_cfg))]
112
113pub use actix_http::body;
114#[cfg(feature = "cookies")]
115pub use cookie;
116
117mod any_body;
118mod builder;
119mod client;
120mod connect;
121pub mod error;
122mod frozen;
123pub mod middleware;
124mod request;
125mod responses;
126mod sender;
127pub mod test;
128pub mod ws;
129
130pub mod http {
131    //! Various HTTP related types.
132
133    // TODO: figure out how best to expose http::Error vs actix_http::Error
134    pub use actix_http::{header, uri, ConnectionType, Error, Method, StatusCode, Uri, Version};
135}
136
137#[allow(deprecated)]
138pub use self::responses::{ClientResponse, JsonBody, MessageBody, ResponseBody};
139pub use self::{
140    builder::ClientBuilder,
141    client::{Client, Connect, Connector},
142    connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse},
143    frozen::{FrozenClientRequest, FrozenSendBuilder},
144    request::ClientRequest,
145    sender::SendClientRequest,
146};
147
148pub(crate) type BoxError = Box<dyn std::error::Error>;