allow send_json to send any serde::Serialize value

This commit is contained in:
soruh
2021-12-11 15:59:34 +01:00
committed by Martin Algesten
parent 56276c3742
commit 59f1fab4d3
3 changed files with 12 additions and 25 deletions

View File

@@ -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<T> = std::result::Result<T, Error>;
/// 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<Response> {
pub fn send_json(mut self, data: impl serde::Serialize) -> Result<Response> {
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.