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>
impl<'a> BodyWithConfig<'a>
Sourcepub fn limit(self, value: u64) -> Self
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).
Sourcepub fn lossy_utf8(self, value: bool) -> Self
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
.
Sourcepub fn reader(self) -> BodyReader<'a> ⓘ
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();
Sourcepub fn read_to_string(self) -> Result<String, Error>
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()?;
Sourcepub fn read_to_vec(self) -> Result<Vec<u8>, Error>
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()?;
Sourcepub fn read_json<T: DeserializeOwned>(self) -> Result<T, Error>
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()?;