diff --git a/src/body.rs b/src/body.rs index 30b2b2a..85888e3 100644 --- a/src/body.rs +++ b/src/body.rs @@ -7,17 +7,12 @@ use crate::response::DEFAULT_CHARACTER_SET; #[cfg(feature = "charset")] use encoding_rs::Encoding; -#[cfg(feature = "json")] -use super::SerdeValue; - /// The different kinds of bodies to send. /// /// *Internal API* pub(crate) enum Payload<'a> { Empty, Text(&'a str, String), - #[cfg(feature = "json")] - JSON(SerdeValue), Reader(Box), Bytes(&'a [u8]), } @@ -27,8 +22,6 @@ impl fmt::Debug for Payload<'_> { match self { Payload::Empty => write!(f, "Empty"), Payload::Text(t, _) => write!(f, "{}", t), - #[cfg(feature = "json")] - Payload::JSON(_) => write!(f, "JSON"), Payload::Reader(_) => write!(f, "Reader"), Payload::Bytes(v) => write!(f, "{:?}", v), } @@ -89,13 +82,6 @@ impl<'a> Payload<'a> { let cursor = Cursor::new(bytes); SizedReader::new(BodySize::Known(len as u64), Box::new(cursor)) } - #[cfg(feature = "json")] - Payload::JSON(v) => { - let bytes = serde_json::to_vec(&v).expect("Bad JSON in payload"); - let len = bytes.len(); - let cursor = Cursor::new(bytes); - SizedReader::new(BodySize::Known(len as u64), Box::new(cursor)) - } Payload::Reader(read) => SizedReader::new(BodySize::Unknown, read), Payload::Bytes(bytes) => { let len = bytes.len(); diff --git a/src/request.rs b/src/request.rs index 33d62bd..e98a6ed 100644 --- a/src/request.rs +++ b/src/request.rs @@ -9,9 +9,6 @@ use crate::unit::{self, Unit}; use crate::Response; use crate::{agent::Agent, error::Error}; -#[cfg(feature = "json")] -use super::SerdeValue; - pub type Result = std::result::Result; /// Request instances are builders that creates a request. @@ -140,11 +137,15 @@ impl Request { /// # } /// ``` #[cfg(feature = "json")] - pub fn send_json(mut self, data: SerdeValue) -> Result { + pub fn send_json(mut self, data: impl serde::Serialize) -> Result { if self.header("Content-Type").is_none() { self = self.set("Content-Type", "application/json"); } - self.do_call(Payload::JSON(data)) + + let json_bytes = serde_json::to_vec(&data) + .expect("Failed to serialze data passed to send_json into JSON"); + + self.do_call(Payload::Bytes(&json_bytes)) } /// Send data as bytes. diff --git a/src/test/body_send.rs b/src/test/body_send.rs index a254835..d64fac6 100644 --- a/src/test/body_send.rs +++ b/src/test/body_send.rs @@ -38,10 +38,10 @@ fn content_length_on_json() { let mut json = SerdeMap::new(); json.insert( "Hello".to_string(), - SerdeValue::String("World!!!".to_string()), + serde_json::Value::String("World!!!".to_string()), ); let resp = post("test://host/content_length_on_json") - .send_json(SerdeValue::Object(json)) + .send_json(serde_json::Value::Object(json)) .unwrap(); let vec = resp.as_write_vec(); let s = String::from_utf8_lossy(&vec); @@ -90,10 +90,10 @@ fn content_type_on_json() { let mut json = SerdeMap::new(); json.insert( "Hello".to_string(), - SerdeValue::String("World!!!".to_string()), + serde_json::Value::String("World!!!".to_string()), ); let resp = post("test://host/content_type_on_json") - .send_json(SerdeValue::Object(json)) + .send_json(serde_json::Value::Object(json)) .unwrap(); let vec = resp.as_write_vec(); let s = String::from_utf8_lossy(&vec); @@ -109,11 +109,11 @@ fn content_type_not_overriden_on_json() { let mut json = SerdeMap::new(); json.insert( "Hello".to_string(), - SerdeValue::String("World!!!".to_string()), + serde_json::Value::String("World!!!".to_string()), ); let resp = post("test://host/content_type_not_overriden_on_json") .set("content-type", "text/plain") - .send_json(SerdeValue::Object(json)) + .send_json(serde_json::Value::Object(json)) .unwrap(); let vec = resp.as_write_vec(); let s = String::from_utf8_lossy(&vec);