adding method to extract separately string, instead of parse
This commit is contained in:
parent
bc6c593776
commit
cc2661d8ac
@ -2,21 +2,23 @@ use http::Request;
|
|||||||
use http_body_util::BodyExt;
|
use http_body_util::BodyExt;
|
||||||
use hyper::body::Incoming;
|
use hyper::body::Incoming;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
use std::{any::TypeId, error::Error};
|
use std::error::Error;
|
||||||
|
|
||||||
pub struct Requester;
|
pub struct Requester;
|
||||||
|
|
||||||
impl Requester {
|
impl Requester {
|
||||||
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error + Send + Sync>>
|
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error + Send + Sync>>
|
||||||
where
|
where
|
||||||
T: DeserializeOwned + 'static,
|
T: DeserializeOwned,
|
||||||
{
|
{
|
||||||
let body = req.collect().await?.to_bytes();
|
let body = req.collect().await?.to_bytes();
|
||||||
if TypeId::of::<T>() == TypeId::of::<String>() {
|
|
||||||
let string = String::from_utf8(body.to_vec())?;
|
|
||||||
return Ok(serde_json::from_str(&string)?);
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(serde_json::from_slice(&body)?)
|
Ok(serde_json::from_slice(&body)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn extract_body_str(
|
||||||
|
req: Request<Incoming>,
|
||||||
|
) -> Result<String, Box<dyn Error + Send + Sync>> {
|
||||||
|
let body = req.collect().await?.to_bytes();
|
||||||
|
Ok(String::from_utf8_lossy(&body).to_string())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user