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::{sink::SinkExt as _, stream::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#![deny(rust_2018_idioms, nonstandard_style)]
104#![warn(future_incompatible)]
105#![allow(
106    clippy::type_complexity,
107    clippy::borrow_interior_mutable_const,
108    clippy::needless_doctest_main
109)]
110#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
111#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
112
113pub use actix_http::body;
114
115#[cfg(feature = "cookies")]
116pub use cookie;
117
118mod any_body;
119mod builder;
120mod client;
121mod connect;
122pub mod error;
123mod frozen;
124pub mod middleware;
125mod request;
126mod responses;
127mod sender;
128pub mod test;
129pub mod ws;
130
131pub mod http {
132    //! Various HTTP related types.
133
134    // TODO: figure out how best to expose http::Error vs actix_http::Error
135    pub use actix_http::{
136        header, uri, ConnectionType, Error, Method, StatusCode, Uri, Version,
137    };
138}
139
140pub use self::builder::ClientBuilder;
141pub use self::client::{Client, Connector};
142pub use self::connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse};
143pub use self::frozen::{FrozenClientRequest, FrozenSendBuilder};
144pub use self::request::ClientRequest;
145#[allow(deprecated)]
146pub use self::responses::{ClientResponse, JsonBody, MessageBody, ResponseBody};
147pub use self::sender::SendClientRequest;
148
149pub(crate) type BoxError = Box<dyn std::error::Error>;