special parse for string
This commit is contained in:
parent
61242718e9
commit
bc6c593776
@ -2,16 +2,21 @@ use http::Request;
|
||||
use http_body_util::BodyExt;
|
||||
use hyper::body::Incoming;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::error::Error;
|
||||
use std::{any::TypeId, error::Error};
|
||||
|
||||
pub struct Requester;
|
||||
|
||||
impl Requester {
|
||||
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error + Send + Sync>>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
T: DeserializeOwned + 'static,
|
||||
{
|
||||
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)?)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user