Crate awc

Source
Expand description

awc is an asynchronous HTTP and WebSocket client library.

§GET Requests

// create client
let mut client = awc::Client::default();

// 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);

§POST Requests

§Raw Body

let mut client = awc::Client::default();
let response = client.post("http://httpbin.org/post")
    .send_body("Raw body contents")
    .await?;

§JSON

let request = serde_json::json!({
    "lang": "rust",
    "body": "json"
});

let mut client = awc::Client::default();
let response = client.post("http://httpbin.org/post")
    .send_json(&request)
    .await?;

§URL Encoded Form

let params = [("foo", "bar"), ("baz", "quux")];

let mut client = awc::Client::default();
let response = client.post("http://httpbin.org/post")
    .send_form(&params)
    .await?;

§Response Compression

All official and common content encoding codecs are supported, optionally.

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.

FeatureCodecs
compress-brotlibrotli
compress-gzipgzip, deflate
compress-zstdzstd

§WebSockets

use futures_util::{SinkExt as _, StreamExt as _};

let (_resp, mut connection) = awc::Client::new()
    .ws("ws://echo.websocket.org")
    .connect()
    .await?;

connection
    .send(awc::ws::Message::Text("Echo".into()))
    .await?;

let response = connection.next().await.unwrap()?;
assert_eq!(response, awc::ws::Frame::Text("Echo".into()));

Re-exports§

pub use cookie;cookies

Modules§

body
Traits and structures to aid consuming and writing HTTP payloads.
error
HTTP client errors
http
Various HTTP related types.
middleware
test
Test helpers for actix http client to use during testing.
ws
Websockets client

Structs§

Client
An asynchronous HTTP and WebSocket client.
ClientBuilder
An HTTP Client builder
ClientRequest
An HTTP Client request builder
ClientResponse
Client Response
Connect
Connector
Manages HTTP client network connectivity.
FrozenClientRequest
FrozenClientRequest struct represents cloneable client request.
FrozenSendBuilder
Builder that allows to modify extra headers.
JsonBody
A Future that reads a body stream, parses JSON, resolving to a deserialized T.
ResponseBody
A Future that reads a body stream, resolving as Bytes.

Enums§

ConnectRequest
Combined HTTP and WebSocket request type received by connection service.
ConnectResponse
Combined HTTP response & WebSocket tunnel type returned from connection service.
SendClientRequest
Future that sends request’s payload and resolves to a server response.

Type Aliases§

BoxConnectorService
BoxedSocket
MessageBodyDeprecated