Crate http_unix_client

Source
Expand description

§http-unix-client

An HTTP client for interacting with HTTP servers over Unix sockets. The crate mimics the architecture of the reqwest crate. The Client is asynchronous (requiring Tokio).

§Supported Platforms

This crate is only supported on Unix-like systems (Linux, macOS, BSD, etc.) because it relies on Unix domain sockets.

§Examples

§Making a GET request

For a single request, you can use the get shortcut method.

let body = get("/tmp/my.socket", "/health")
    .await?
    .text()
    .await?;

println!("body = {body:?}");

NOTE: If you plan to perform multiple requests, it is best to create a Client and reuse it, taking advantage of keep-alive connection pooling.

§Making POST requests (or setting request bodies)

There are several ways you can set the body of a request. The basic one is by using the body() method of a RequestBuilder. This lets you set the exact raw bytes of what the body should be. It accepts various types, including String and Vec<u8>. If you wish to pass a custom type, you can use the reqwest::Body constructors.


let client = Client::new();
let res = client.post("/tmp/my.socket", "/health")
    .body("the exact body that is sent")
    .send()
    .await?;

§Forms

It’s very common to want to send form data in a request body. This can be done with any type that can be serialized into form data.

This can be an array of tuples, or a HashMap, or a custom type that implements Serialize.

// This will POST a body of `foo=bar&baz=quux`
let params = [("foo", "bar"), ("baz", "quux")];
let client = Client::new();
let res = client.post("/tmp/my.socket", "/health")
    .form(&params)
    .send()
    .await?;
    Ok(())
}

§JSON

There is also a json method helper on the RequestBuilder that works in a similar fashion the form method. It can take any value that can be serialized into JSON. The feature json is required.

// This will POST a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");
let client = Client::new();
let res = client.post("/tmp/my.socket", "/health")
    .json(&map)
    .send()
    .await?;

Modules§

header
HTTP header types

Structs§

Body
Represents the body of an HTTP request.
Client
An asynchronous Client to make Requests over Unix socket with.
Cookie
Representation of an HTTP cookie.
Extensions
A type map of protocol extensions.
Method
The Request Method (VERB)
Request
A request which can be executed with Client::execute().
RequestBuilder
A builder to construct the properties of a Request.
Response
A Response to a submitted Request.
StatusCode
An HTTP status code (status-code in RFC 9110 et al.).
UnixUrl
A wrapper around Url representing a URL over a UNIX domain socket.
Uri
The URI component of a request.
Url
A parsed URL record.
Version
Represents a version of the HTTP spec.

Enums§

Error
A unified error type for all operations in this crate.

Functions§

get
Shortcut method to quickly make a GET request.

Type Aliases§

Result
A result type alias for this crate.