Struct reqwest::blocking::Body

source ·
pub struct Body { /* private fields */ }
Expand description

The body of a Request.

In most cases, this is not needed directly, as the RequestBuilder.body method uses Into<Body>, which allows passing many things (like a string or vector of bytes).

Implementations§

source§

impl Body

source

pub fn new<R: Read + Send + 'static>(reader: R) -> Body

Instantiate a Body from a reader.

§Note

While allowing for many types to be used, these bodies do not have a way to reset to the beginning and be reused. This means that when encountering a 307 or 308 status code, instead of repeating the request at the new location, the Response will be returned with the redirect status code set.

let file = File::open("national_secrets.txt")?;
let body = Body::new(file);

If you have a set of bytes, like String or Vec<u8>, using the From implementations for Body will store the data in a manner it can be reused.

let s = "A stringy body";
let body = Body::from(s);
source

pub fn sized<R: Read + Send + 'static>(reader: R, len: u64) -> Body

Create a Body from a Read where the size is known in advance but the data should not be fully loaded into memory. This will set the Content-Length header and stream from the Read.

let file = File::open("a_large_file.txt")?;
let file_size = file.metadata()?.len();
let body = Body::sized(file, file_size);
source

pub fn as_bytes(&self) -> Option<&[u8]>

Returns the body as a byte slice if the body is already buffered in memory. For streamed requests this method returns None.

source

pub fn buffer(&mut self) -> Result<&[u8], Error>

Converts streamed requests to their buffered equivalent and returns a reference to the buffer. If the request is already buffered, this has no effect.

Be aware that for large requests this method is expensive and may cause your program to run out of memory.

Trait Implementations§

source§

impl Debug for Body

source§

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

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

impl From<&'static [u8]> for Body

source§

fn from(s: &'static [u8]) -> Body

Converts to this type from the input type.
source§

impl From<&'static str> for Body

source§

fn from(s: &'static str) -> Body

Converts to this type from the input type.
source§

impl From<Bytes> for Body

source§

fn from(b: Bytes) -> Body

Converts to this type from the input type.
source§

impl From<File> for Body

source§

fn from(f: File) -> Body

Converts to this type from the input type.
source§

impl From<String> for Body

source§

fn from(s: String) -> Body

Converts to this type from the input type.
source§

impl From<Vec<u8>> for Body

source§

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

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for Body

§

impl !RefUnwindSafe for Body

§

impl Send for Body

§

impl !Sync for Body

§

impl Unpin for Body

§

impl !UnwindSafe for Body

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

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
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T