Skip to main content

qubit_http/request/
http_request_body.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! Request body variants.
11
12use std::fmt;
13
14use bytes::Bytes;
15use serde::{
16    Deserialize,
17    Serialize,
18};
19use strum::{
20    Display,
21    EnumString,
22};
23
24/// Encodes how the outbound body is represented before sending via reqwest.
25#[derive(Clone, PartialEq, Eq, Default, Serialize, Deserialize, Display, EnumString)]
26#[serde(rename_all = "snake_case")]
27#[strum(serialize_all = "snake_case")]
28pub enum HttpRequestBody {
29    /// No body (typical for GET/HEAD).
30    #[default]
31    Empty,
32    /// Opaque binary payload.
33    #[strum(disabled)]
34    Bytes(Bytes),
35    /// UTF-8 text; builders may set `Content-Type: text/plain`.
36    #[strum(disabled)]
37    Text(String),
38    /// JSON-serialized bytes; builders may set `Content-Type: application/json`.
39    #[strum(disabled)]
40    Json(Bytes),
41    /// URL-encoded form bytes; builders may set `Content-Type: application/x-www-form-urlencoded`.
42    #[strum(disabled)]
43    Form(Bytes),
44    /// Multipart body bytes; builders ensure multipart Content-Type boundary consistency.
45    #[strum(disabled)]
46    Multipart(Bytes),
47    /// NDJSON bytes (`\n`-delimited JSON objects); builders may set `application/x-ndjson`.
48    #[strum(disabled)]
49    Ndjson(Bytes),
50    /// Chunked upload payload represented as ordered byte chunks.
51    ///
52    /// The client sends this variant through reqwest streaming body support.
53    #[strum(disabled)]
54    Stream(Vec<Bytes>),
55}
56
57impl fmt::Debug for HttpRequestBody {
58    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
59        match self {
60            Self::Empty => formatter.write_str("Empty"),
61            Self::Bytes(bytes) => formatter.debug_struct("Bytes").field("len", &bytes.len()).finish(),
62            Self::Text(text) => formatter.debug_struct("Text").field("len", &text.len()).finish(),
63            Self::Json(bytes) => formatter.debug_struct("Json").field("len", &bytes.len()).finish(),
64            Self::Form(bytes) => formatter.debug_struct("Form").field("len", &bytes.len()).finish(),
65            Self::Multipart(bytes) => formatter.debug_struct("Multipart").field("len", &bytes.len()).finish(),
66            Self::Ndjson(bytes) => formatter.debug_struct("Ndjson").field("len", &bytes.len()).finish(),
67            Self::Stream(chunks) => formatter
68                .debug_struct("Stream")
69                .field("chunks", &chunks.len())
70                .field("len", &chunks.iter().map(Bytes::len).sum::<usize>())
71                .finish(),
72        }
73    }
74}