2022-03-05 23:15:33 +00:00
|
|
|
//! `awc` is an asynchronous HTTP and WebSocket client library.
|
2020-09-09 16:24:12 +03:00
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! # `GET` Requests
|
2021-01-08 12:00:58 +00:00
|
|
|
//! ```no_run
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
2022-03-08 16:51:40 +00:00
|
|
|
//! // create client
|
2020-09-09 16:24:12 +03:00
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! // construct request
|
|
|
|
//! let req = client.get("http://www.rust-lang.org")
|
|
|
|
//! .insert_header(("User-Agent", "awc/3.0"));
|
|
|
|
//!
|
|
|
|
//! // send request and await response
|
|
|
|
//! let res = req.send().await?;
|
|
|
|
//! println!("Response: {:?}", res);
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! # `POST` Requests
|
|
|
|
//! ## Raw Body
|
2021-01-08 12:00:58 +00:00
|
|
|
//! ```no_run
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
|
|
|
//! .send_body("Raw body contents")
|
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! ## JSON
|
2021-01-08 12:00:58 +00:00
|
|
|
//! ```no_run
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
2022-03-08 16:51:40 +00:00
|
|
|
//! let request = serde_json::json!({
|
|
|
|
//! "lang": "rust",
|
|
|
|
//! "body": "json"
|
|
|
|
//! });
|
2020-09-09 16:24:12 +03:00
|
|
|
//!
|
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
2022-03-08 16:51:40 +00:00
|
|
|
//! .send_json(&request)
|
2020-09-09 16:24:12 +03:00
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
2019-03-27 09:24:55 -07:00
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! ## URL Encoded Form
|
2021-01-08 12:00:58 +00:00
|
|
|
//! ```no_run
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), awc::error::SendRequestError> {
|
2022-03-08 16:51:40 +00:00
|
|
|
//! let params = [("foo", "bar"), ("baz", "quux")];
|
2019-11-26 11:25:50 +06:00
|
|
|
//!
|
2020-09-09 16:24:12 +03:00
|
|
|
//! let mut client = awc::Client::default();
|
|
|
|
//! let response = client.post("http://httpbin.org/post")
|
2022-03-08 16:51:40 +00:00
|
|
|
//! .send_form(¶ms)
|
2020-09-09 16:24:12 +03:00
|
|
|
//! .await?;
|
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
2019-03-27 09:24:55 -07:00
|
|
|
//! ```
|
2020-09-09 16:24:12 +03:00
|
|
|
//!
|
2021-06-19 20:23:06 +01:00
|
|
|
//! # Response Compression
|
|
|
|
//! All [official][iana-encodings] and common content encoding codecs are supported, optionally.
|
2020-09-09 16:24:12 +03:00
|
|
|
//!
|
2021-06-19 20:23:06 +01:00
|
|
|
//! The `Accept-Encoding` header will automatically be populated with enabled codecs and added to
|
|
|
|
//! outgoing requests, allowing servers to select their `Content-Encoding` accordingly.
|
|
|
|
//!
|
|
|
|
//! Feature flags enable these codecs according to the table below. By default, all `compress-*`
|
|
|
|
//! features are enabled.
|
|
|
|
//!
|
|
|
|
//! | Feature | Codecs |
|
|
|
|
//! | ----------------- | ------------- |
|
|
|
|
//! | `compress-brotli` | brotli |
|
|
|
|
//! | `compress-gzip` | gzip, deflate |
|
|
|
|
//! | `compress-zstd` | zstd |
|
|
|
|
//!
|
|
|
|
//! [iana-encodings]: https://www.iana.org/assignments/http-parameters/http-parameters.xhtml#content-coding
|
|
|
|
//!
|
2022-03-08 16:51:40 +00:00
|
|
|
//! # WebSockets
|
2021-01-08 12:00:58 +00:00
|
|
|
//! ```no_run
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # #[actix_rt::main]
|
|
|
|
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2022-12-18 01:34:48 +00:00
|
|
|
//! use futures_util::{SinkExt as _, StreamExt as _};
|
2022-03-08 16:51:40 +00:00
|
|
|
//!
|
2020-09-09 16:24:12 +03:00
|
|
|
//! let (_resp, mut connection) = awc::Client::new()
|
|
|
|
//! .ws("ws://echo.websocket.org")
|
|
|
|
//! .connect()
|
|
|
|
//! .await?;
|
|
|
|
//!
|
|
|
|
//! connection
|
2021-01-04 11:27:32 +00:00
|
|
|
//! .send(awc::ws::Message::Text("Echo".into()))
|
2020-09-09 16:24:12 +03:00
|
|
|
//! .await?;
|
2022-03-08 16:51:40 +00:00
|
|
|
//!
|
2020-09-09 16:24:12 +03:00
|
|
|
//! let response = connection.next().await.unwrap()?;
|
2022-03-08 16:51:40 +00:00
|
|
|
//! assert_eq!(response, awc::ws::Frame::Text("Echo".into()));
|
2020-09-09 16:24:12 +03:00
|
|
|
//! # Ok(())
|
|
|
|
//! # }
|
|
|
|
//! ```
|
|
|
|
|
2021-12-08 06:09:56 +00:00
|
|
|
#![deny(rust_2018_idioms, nonstandard_style)]
|
|
|
|
#![warn(future_incompatible)]
|
2020-10-30 02:50:53 +00:00
|
|
|
#![allow(
|
|
|
|
clippy::type_complexity,
|
|
|
|
clippy::borrow_interior_mutable_const,
|
2023-12-16 10:26:32 +00:00
|
|
|
clippy::needless_doctest_main
|
2020-10-30 02:50:53 +00:00
|
|
|
)]
|
|
|
|
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
|
|
|
|
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
|
2023-02-26 21:55:25 +00:00
|
|
|
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
|
2020-10-30 02:50:53 +00:00
|
|
|
|
2021-12-25 02:28:23 +00:00
|
|
|
pub use actix_http::body;
|
|
|
|
#[cfg(feature = "cookies")]
|
|
|
|
pub use cookie;
|
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
mod any_body;
|
2019-03-25 21:58:01 -07:00
|
|
|
mod builder;
|
2021-10-26 07:37:40 +08:00
|
|
|
mod client;
|
2019-03-25 21:58:01 -07:00
|
|
|
mod connect;
|
2019-03-27 18:53:19 -07:00
|
|
|
pub mod error;
|
2019-09-12 10:40:56 +06:00
|
|
|
mod frozen;
|
2021-02-28 10:17:08 -08:00
|
|
|
pub mod middleware;
|
2019-03-25 21:58:01 -07:00
|
|
|
mod request;
|
2021-12-25 02:33:37 +00:00
|
|
|
mod responses;
|
2019-09-12 10:40:56 +06:00
|
|
|
mod sender;
|
2019-03-26 21:54:57 -07:00
|
|
|
pub mod test;
|
2019-03-29 13:49:21 -07:00
|
|
|
pub mod ws;
|
2019-03-25 21:58:01 -07:00
|
|
|
|
2021-12-25 02:28:23 +00:00
|
|
|
pub mod http {
|
|
|
|
//! Various HTTP related types.
|
|
|
|
|
|
|
|
// TODO: figure out how best to expose http::Error vs actix_http::Error
|
2023-07-17 02:38:12 +01:00
|
|
|
pub use actix_http::{header, uri, ConnectionType, Error, Method, StatusCode, Uri, Version};
|
2021-12-25 02:28:23 +00:00
|
|
|
}
|
2021-10-26 07:37:40 +08:00
|
|
|
|
2021-12-25 02:33:37 +00:00
|
|
|
#[allow(deprecated)]
|
|
|
|
pub use self::responses::{ClientResponse, JsonBody, MessageBody, ResponseBody};
|
2023-07-17 02:38:12 +01:00
|
|
|
pub use self::{
|
|
|
|
builder::ClientBuilder,
|
|
|
|
client::{Client, Connect, Connector},
|
|
|
|
connect::{BoxConnectorService, BoxedSocket, ConnectRequest, ConnectResponse},
|
|
|
|
frozen::{FrozenClientRequest, FrozenSendBuilder},
|
|
|
|
request::ClientRequest,
|
|
|
|
sender::SendClientRequest,
|
|
|
|
};
|
2019-03-25 21:58:01 -07:00
|
|
|
|
2021-12-04 19:40:47 +00:00
|
|
|
pub(crate) type BoxError = Box<dyn std::error::Error>;
|