http-request 8.91.109

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::*;

/// Default implementation for Body.
///
/// # Returns
///
/// - `Body` - Returns a Body with empty text content.
impl Default for Body {
    #[inline(always)]
    fn default() -> Self {
        Self::Text(EMPTY_STR.to_owned())
    }
}

/// Formats the Body for display.
///
/// # Arguments
///
/// - `&mut Formatter<'_>` - The formatter to write to.
///
/// # Returns
///
/// - `fmt::Result` - Result of the formatting operation.
impl Display for Body {
    #[inline(always)]
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Self::Text(text) => write!(f, "{text}"),
            Self::Json(json) => write!(
                f,
                "{}",
                serde_json::to_string(json).unwrap_or_else(|_| String::from("{}"))
            ),
            Self::Binary(binary) => write!(f, "{binary:?}"),
        }
    }
}

/// Serializes the Body content.
///
/// # Arguments
///
/// - `S` - The type of the serializer.
///
/// # Returns
///
/// - `Result<S::Ok, S::Error>` - Result of the serialization.
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) => json.serialize(serializer),
            Self::Binary(binary) => binary.serialize(serializer),
        }
    }
}