pub struct Body(/* private fields */);Expand description
The body of a request or a response. It is empty, bytes already in memory, or a stream read as fast as the platform delivers it.
The fields that come after a body — its trailers — are read with trailers
once the body has ended: it reads what is left of the body first, discarding those bytes to
reach its end, as HTTP sends its trailers only after the last byte, so asking for them never
waits for a reader that never comes. A request carries its own with
with_trailers, which the body answers once its bytes have been read,
so the fields may be made from the bytes streamed (a checksum). Asking again answers the same
fields. A body made from bytes can be sent again with try_clone, which is
what a redirect that resends its body needs.
Implementations§
Source§impl Body
impl Body
Sourcepub fn from_stream(stream: impl BodyStream + 'static) -> Body
pub fn from_stream(stream: impl BodyStream + 'static) -> Body
The bytes stream reads, as they come.
Sourcepub fn with_trailers(self, trailers: HeaderMap) -> Body
pub fn with_trailers(self, trailers: HeaderMap) -> Body
trailers, sent after this body ends: the body answers them once its bytes have been
read, as HTTP sends its trailers after the last byte. A body whose stream has fields of
its own has them replaced. A client whose protocol layer cannot send trailers fails the
request rather than dropping them. Over HTTP/1.1 the request names its trailers in a
Trailer header, as RFC 9110 §6.6.2 asks of a sender: a client may drop a field it was
not told of (hyper does), and a body’s trailers may not be known until it ends, so the
request says them, not the client.
Sourcepub fn try_clone(&self) -> Option<Body>
pub fn try_clone(&self) -> Option<Body>
A second body with the same bytes, for a request a client sends again on a redirect that
resends its body. Some for a body made from bytes and for an empty one, with the fields
it sends; None for one that streams, whose bytes are gone once they are read and whose
fields may depend on them.
Sourcepub fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> BodyReadFuture<'a>
pub fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> BodyReadFuture<'a>
Reads the next bytes of the body into buf, answering how many it read, 0 at the end:
a piece at a time, as the platform delivers it.
Sourcepub fn read_all(&mut self) -> BodyReadAllFuture<'_>
pub fn read_all(&mut self) -> BodyReadAllFuture<'_>
The whole body, read through: the bytes a request carries, or a response’s.
Sourcepub fn trailers(&mut self) -> BodyTrailersFuture<'_>
pub fn trailers(&mut self) -> BodyTrailersFuture<'_>
The fields that came after the body, once it has ended: reads what is left of the body
first, discarding those bytes to reach its end, as HTTP sends its trailers only after its
last byte, so asking for them never waits for a reader that never comes. The bytes are
discarded into a buffer on the stack, so a body that has already ended costs no allocation.
Asking again answers the same fields, which a client that sends them keeps; None where
the response has none, and always None on a client whose protocol layer does not expose
them.