Struct BodyWithConfig

Source
pub struct BodyWithConfig<'a> { /* private fields */ }
Expand description

Configuration of how to read the body.

Obtained via one of:

§Handling large responses

The BodyWithConfig is the primary way to increase the default 10MB size limit when downloading large files to memory:

// Download a 50MB file
let large_data = ureq::get("http://httpbin.org/bytes/200000000")
    .call()?
    .body_mut()
    .with_config()
    .limit(50 * 1024 * 1024) // 50MB
    .read_to_vec()?;

Implementations§

Source§

impl<'a> BodyWithConfig<'a>

Source

pub fn limit(self, value: u64) -> Self

Limit the response body.

Controls how many bytes we should read before throwing an error. This is used to ensure RAM isn’t exhausted by a server sending a very large response body.

The default limit is u64::MAX (unlimited).

Source

pub fn lossy_utf8(self, value: bool) -> Self

Replace invalid utf-8 chars.

true means that broken utf-8 characters are replaced by a question mark ? (not utf-8 replacement char). This happens after charset conversion regardless of whether the charset feature is enabled or not.

The default is false.

Source

pub fn reader(self) -> BodyReader<'a>

Creates a reader.

The reader is either shared or owned, depending on with_config or into_with_config.

§Example of owned vs shared
// Creates an owned reader.
let reader = ureq::get("https://httpbin.org/get")
    .call()?
    .into_body()
    // takes ownership of Body
    .into_with_config()
    .limit(10)
    .reader();
// Creates a shared reader.
let reader = ureq::get("https://httpbin.org/get")
    .call()?
    .body_mut()
    // borrows Body
    .with_config()
    .limit(10)
    .reader();
Source

pub fn read_to_string(self) -> Result<String, Error>

Read into string.

Caution: without a preceeding limit(), this becomes an unbounded sized String. A bad server could exhaust your memory.

§Example
// Reads max 10k to a String.
let string = ureq::get("https://httpbin.org/get")
    .call()?
    .body_mut()
    .with_config()
    // Important. Limits body to 10k
    .limit(10_000)
    .read_to_string()?;
Source

pub fn read_to_vec(self) -> Result<Vec<u8>, Error>

Read into vector.

Caution: without a preceeding limit(), this becomes an unbounded sized Vec. A bad server could exhaust your memory.

§Example
// Reads max 10k to a Vec.
let myvec = ureq::get("https://httpbin.org/get")
    .call()?
    .body_mut()
    .with_config()
    // Important. Limits body to 10k
    .limit(10_000)
    .read_to_vec()?;
Source

pub fn read_json<T: DeserializeOwned>(self) -> Result<T, Error>

Read JSON body.

Caution: without a preceeding limit(), this becomes an unbounded sized String. A bad server could exhaust your memory.

§Example
use serde_json::Value;

// Reads max 10k as a JSON value.
let json: Value  = ureq::get("https://httpbin.org/get")
    .call()?
    .body_mut()
    .with_config()
    // Important. Limits body to 10k
    .limit(10_000)
    .read_json()?;

Auto Trait Implementations§

§

impl<'a> Freeze for BodyWithConfig<'a>

§

impl<'a> !RefUnwindSafe for BodyWithConfig<'a>

§

impl<'a> Send for BodyWithConfig<'a>

§

impl<'a> Sync for BodyWithConfig<'a>

§

impl<'a> Unpin for BodyWithConfig<'a>

§

impl<'a> !UnwindSafe for BodyWithConfig<'a>

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, 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<T> ErasedDestructor for T
where T: 'static,