Skip to content

Commit

Permalink
Improve doc for BodyWithConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
algesten committed Jan 22, 2025
1 parent 98057ad commit 5118d6c
Showing 1 changed file with 55 additions and 2 deletions.
57 changes: 55 additions & 2 deletions src/body/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ impl Body {
///
/// * Response is limited to 10MB.
///
/// To change this default use [`Body::as_reader()`] and deserialize JSON manually.
/// To change this default use [`Body::with_config()`].
///
/// The returned value is something that derives [`Deserialize`](serde::Deserialize).
/// You might need to be explicit with which type you want. See example below.
Expand Down Expand Up @@ -387,7 +387,7 @@ impl<'a> BodyWithConfig<'a> {
///
/// The reader is either shared or owned, depending on `with_config` or `into_with_config`.
///
/// Consider:
/// ## Example of owned vs shared
///
/// ```
/// // Creates an owned reader.
Expand Down Expand Up @@ -417,6 +417,23 @@ impl<'a> BodyWithConfig<'a> {
}

/// Read into string.
///
/// *Caution:* without a preceeding [`limit()`][BodyWithConfig::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()?;
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn read_to_string(self) -> Result<String, Error> {
use std::io::Read;
let mut reader = self.do_build();
Expand All @@ -426,6 +443,23 @@ impl<'a> BodyWithConfig<'a> {
}

/// Read into vector.
///
/// *Caution:* without a preceeding [`limit()`][BodyWithConfig::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()?;
/// # Ok::<_, ureq::Error>(())
/// ```
pub fn read_to_vec(self) -> Result<Vec<u8>, Error> {
use std::io::Read;
let mut reader = self.do_build();
Expand All @@ -435,6 +469,25 @@ impl<'a> BodyWithConfig<'a> {
}

/// Read JSON body.
///
/// *Caution:* without a preceeding [`limit()`][BodyWithConfig::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()?;
/// # Ok::<_, ureq::Error>(())
/// ```
#[cfg(feature = "json")]
pub fn read_json<T: serde::de::DeserializeOwned>(self) -> Result<T, Error> {
let reader = self.do_build();
Expand Down

0 comments on commit 5118d6c

Please sign in to comment.