Expand description
§http-client-unix-domain-socket
A simple HTTP (json) client using UNIX domain socket in Rust
This Rust crate provides a Unix socket HTTP client for asynchronous communication with local servers. It enables seamless interaction via Unix domain sockets using Tokio and Hyper, supporting raw HTTP requests and optional JSON serialization through feature flags. The client handles connection management, request building, response parsing, and error handling, including reconnection logic.
§Examples
§HTTP GET
use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponse};
pub async fn get_hello_world() {
let mut client = ClientUnix::try_new("/tmp/unix.socket")
.await
.expect("ClientUnix::try_new");
match client
.send_request("/nolanv", Method::GET, &vec![("Host", "localhost")], None)
.await
{
Err(ErrorAndResponse::ResponseUnsuccessful(status_code, response)) => {
assert!(status_code == StatusCode::NOT_FOUND);
assert!(response == "not found".as_bytes());
}
Ok((status_code, response)) => {
assert_eq!(status_code, StatusCode::OK);
assert_eq!(response, "Hello nolanv".as_bytes());
}
Err(_) => panic!("Something went wrong")
}
}
§HTTP POST JSON (feature = json)
use http_client_unix_domain_socket::{ClientUnix, Method, StatusCode, ErrorAndResponseJson};
use serde::{Deserialize, Serialize};
#[derive(Serialize)]
struct NameJson {
name: String,
}
#[derive(Deserialize)]
struct HelloJson {
hello: String,
}
#[derive(Deserialize, Debug)]
struct ErrorJson {
msg: String,
}
pub async fn post_hello_world() {
let mut client = ClientUnix::try_new("/tmp/unix.socket")
.await
.expect("ClientUnix::try_new");
match client
.send_request_json::<NameJson, HelloJson, ErrorJson>(
"/nolanv",
Method::POST,
&vec![("Host", "localhost")],
Some(&NameJson { name: "nolanv".into() }))
.await
{
Err(ErrorAndResponseJson::ResponseUnsuccessful(status_code, response)) => {
assert!(status_code == StatusCode::BAD_REQUEST);
assert!(response.msg == "bad request");
}
Ok((status_code, response)) => {
assert_eq!(status_code, StatusCode::OK);
assert_eq!(response.hello, "nolanv");
}
Err(_) => panic!("Something went wrong")
}
}
§Feature flags
json
(default): Addsend_request_json
which enable automatic parsing of request/response body withserde_json
and addContent-Type
header.
Structs§
- Body
- The body type used in axum requests and responses.
- Client
Unix - A simple HTTP (json) client using UNIX domain socket in Rust
- Method
- The Request Method (VERB)
- Status
Code - An HTTP status code (
status-code
in RFC 9110 et al.).
Enums§
- Error
- Internal Error, wrapping other source of error.
- Error
AndResponse - Error used by crate::ClientUnix::send_request to be able to return unsuccessful HTTP error body.
- Error
AndResponse Json - Error used by crate::ClientUnix::send_request_json to be able to return unsuccessful HTTP error typed body (feature = json).