http-request 8.58.0

http-request is a lightweight, efficient library for building, sending, and handling HTTP/HTTPS requests in Rust applications. It provides a simple and intuitive API, allowing developers to easily interact with web services, whether they use the "HTTP" or "HTTPS" protocol. The library supports various HTTP methods, custom headers, request bodies, timeout, automatic handling of redirects (including detecting redirect loops), and enhanced response body decoding (both automatic and manual), enabling fast and secure communication. Whether working with secure "HTTPS" connections or standard "HTTP" requests, the library is optimized for performance, minimal resource usage, and easy integration into Rust projects.
Documentation
use crate::*;

impl Default for Body {
    fn default() -> Self {
        Self::Text(EMPTY_STR)
    }
}

impl Display for Body {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Text(text) => write!(f, "{}", text.to_string()),
            Self::Json(json) => {
                let hash_map: HashMap<_, _> = json
                    .iter()
                    .map(|entry| (*entry.key(), *entry.value()))
                    .collect();
                write!(
                    f,
                    "{}",
                    serde_json::to_string(&hash_map).unwrap_or_else(|_| String::from("{}"))
                )
            }
            Self::Binary(binary) => write!(f, "{:?}", binary),
        }
    }
}

impl Serialize for Body {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Text(text) => text.serialize(serializer),
            Self::Json(json) => {
                let hash_map: HashMap<_, _> = json
                    .iter()
                    .map(|entry| (*entry.key(), *entry.value()))
                    .collect();
                hash_map.serialize(serializer)
            }
            Self::Binary(binary) => binary.serialize(serializer),
        }
    }
}