diff --git a/src/client.rs b/src/client.rs index 319d987..d25e609 100644 --- a/src/client.rs +++ b/src/client.rs @@ -612,16 +612,16 @@ impl RequestBuilder { /// /// let client = reqwest::Client::new()?; /// let res = client.post("http://httpbin.org") - /// .json(&map) + /// .json(&map)? /// .send()?; /// # Ok(()) /// # } /// ``` - pub fn json(mut self, json: &T) -> RequestBuilder { - let body = serde_json::to_vec(json).expect("serde to_vec cannot fail"); + pub fn json(mut self, json: &T) -> ::Result { + let body = serde_json::to_vec(json).map_err(::error::from)?; self.headers.set(ContentType::json()); self.body = Some(Ok(body.into())); - self + Ok(self) } /// Build a `Request`, which can be inspected, modified and executed with diff --git a/src/lib.rs b/src/lib.rs index e9080af..6de09ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,7 +101,7 @@ //! //! let client = reqwest::Client::new()?; //! let res = client.post("http://httpbin.org/post") -//! .json(&map) +//! .json(&map)? //! .send()?; //! # Ok(()) //! # }