Skip to main content

RequestBody

Enum RequestBody 

Source
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: BoxBytesStream

The stream of body chunks.

§length: Option<usize>

Known length, if available.

Implementations§

Source§

impl RequestBody

Source

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).

Source

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.

Source

pub fn has_known_length(&self) -> bool

Returns true if the body length is known.

Source

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.

Source

pub fn replay_class(&self) -> ReplayClass

Returns the replayability classification for this body.

Source

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.

Source

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.

Source

pub fn from_stream<S>(stream: S, length: Option<usize>) -> Self
where S: Stream<Item = Result<Bytes>> + Send + 'static,

Create a stream body with known length.

Trait Implementations§

Source§

impl Debug for RequestBody

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RequestBody

Source§

fn default() -> RequestBody

Returns the “default value” for a type. Read more
Source§

impl From<&[u8]> for RequestBody

Source§

fn from(s: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for RequestBody

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl From<Bytes> for RequestBody

Source§

fn from(b: Bytes) -> Self

Converts to this type from the input type.
Source§

impl From<String> for RequestBody

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for RequestBody

Source§

fn from(v: Vec<u8>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more