Struct Body

struct Body { ... }

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

impl Body

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.

# use std::fs::File;
# use reqwest::blocking::Body;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("national_secrets.txt")?;
let body = Body::new(file);
# Ok(())
# }

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.

# use reqwest::blocking::Body;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let s = "A stringy body";
let body = Body::from(s);
# Ok(())
# }
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.

# use std::fs::File;
# use reqwest::blocking::Body;
# fn run() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("a_large_file.txt")?;
let file_size = file.metadata()?.len();
let body = Body::sized(file, file_size);
# Ok(())
# }
fn as_bytes(self: &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.

fn buffer(self: &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.

impl Debug for Body

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

impl Freeze for Body

impl From for Body

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

impl From for Body

fn from(b: Bytes) -> Body

impl From for Body

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

impl From for Body

fn from(f: File) -> Body

impl From for Body

fn from(s: String) -> Body

impl From for Body

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

impl RefUnwindSafe for Body

impl Send for Body

impl Sync for Body

impl Unpin for Body

impl UnsafeUnpin for Body

impl UnwindSafe for Body

impl<T> Any for Body

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Body

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Body

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

impl<T> ErasedDestructor for Body

impl<T> From for Body

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Body

impl<T> PolicyExt for Body

fn and<P, B, E>(self: Self, other: P) -> And<T, P>
where
    T: Policy<B, E>,
    P: Policy<B, E>
fn or<P, B, E>(self: Self, other: P) -> Or<T, P>
where
    T: Policy<B, E>,
    P: Policy<B, E>

impl<T> WithSubscriber for Body

impl<T, U> Into for Body

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for Body

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

impl<T, U> TryInto for Body

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