handle json serialization errors
fixes https://github.com/seanmonstar/reqwest/issues/112
This commit is contained in:
@@ -612,16 +612,16 @@ impl RequestBuilder {
|
|||||||
///
|
///
|
||||||
/// let client = reqwest::Client::new()?;
|
/// let client = reqwest::Client::new()?;
|
||||||
/// let res = client.post("http://httpbin.org")
|
/// let res = client.post("http://httpbin.org")
|
||||||
/// .json(&map)
|
/// .json(&map)?
|
||||||
/// .send()?;
|
/// .send()?;
|
||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn json<T: Serialize>(mut self, json: &T) -> RequestBuilder {
|
pub fn json<T: Serialize>(mut self, json: &T) -> ::Result<RequestBuilder> {
|
||||||
let body = serde_json::to_vec(json).expect("serde to_vec cannot fail");
|
let body = serde_json::to_vec(json).map_err(::error::from)?;
|
||||||
self.headers.set(ContentType::json());
|
self.headers.set(ContentType::json());
|
||||||
self.body = Some(Ok(body.into()));
|
self.body = Some(Ok(body.into()));
|
||||||
self
|
Ok(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build a `Request`, which can be inspected, modified and executed with
|
/// Build a `Request`, which can be inspected, modified and executed with
|
||||||
|
|||||||
@@ -101,7 +101,7 @@
|
|||||||
//!
|
//!
|
||||||
//! let client = reqwest::Client::new()?;
|
//! let client = reqwest::Client::new()?;
|
||||||
//! let res = client.post("http://httpbin.org/post")
|
//! let res = client.post("http://httpbin.org/post")
|
||||||
//! .json(&map)
|
//! .json(&map)?
|
||||||
//! .send()?;
|
//! .send()?;
|
||||||
//! # Ok(())
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
|
|||||||
Reference in New Issue
Block a user