pub enum RequestBody {
Empty,
Bytes(Bytes),
Stream {
stream: BoxBytesStream,
length: Option<usize>,
},
}Expand description
Request body.
Supports empty bodies, fixed-size byte buffers, and async streams.
Stream bodies are piped through to the transport incrementally:
each chunk is sent as soon as it is produced, with no eager buffering.
The optional length hint drives Content-Length for known-size
streams; unknown-length streams use HTTP/1.1 chunked transfer encoding
(selected by hyper).
Variants§
Empty
Empty body.
Bytes(Bytes)
Byte body with known length.
Stream
Async stream body.
Chunks are sent incrementally as the stream produces them. If
length is Some(n), a Content-Length: n header is set when
the user has not supplied a conflicting value. If length is
None, no Content-Length is set; the transport selects a
safe transfer mode (chunked for HTTP/1.1).
Fields
stream: BoxBytesStreamThe stream of body chunks.
Implementations§
Source§impl RequestBody
impl RequestBody
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the body is empty.
For Stream bodies the length hint is not authoritative — a
Some(0) stream may still produce bytes and an unbounded stream
may be empty. This method returns false for all stream bodies;
callers must not use it to decide Content-Length (see
Self::len and Self::has_known_length).
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the known length of the body, if available.
Stream bodies may not have a known length until fully consumed.
Returns 0 for unknown-length streams; callers that need to
distinguish “empty” from “unknown length” must check
has_known_length first.
Sourcepub fn has_known_length(&self) -> bool
pub fn has_known_length(&self) -> bool
Returns true if the body length is known.
Sourcepub fn is_replayable(&self) -> bool
pub fn is_replayable(&self) -> bool
Returns true if this body is replayable (can be sent multiple times).
Only byte bodies are replayable. Streams are consumed on use.
Sourcepub fn replay_class(&self) -> ReplayClass
pub fn replay_class(&self) -> ReplayClass
Returns the replayability classification for this body.
Sourcepub fn try_clone_for_redirect(&self) -> Result<Self>
pub fn try_clone_for_redirect(&self) -> Result<Self>
Attempt to clone this body for use in a redirect request.
Returns a cloned body if the body is replayable (empty or bytes), or an error if the body is a live stream that cannot be resent.
§Errors
Returns Error::BodyNotReplayableForRedirect if the body is a
live stream that cannot be replayed for a redirect.
Sourcepub async fn into_bytes(self) -> Result<Bytes>
pub async fn into_bytes(self) -> Result<Bytes>
Consume the body and return all bytes.
For byte bodies, returns the bytes directly. For stream bodies, collects all chunks. For empty bodies, returns empty bytes.
§Errors
Returns an error if a stream chunk fails.