Files

35 lines
880 B
Rust

use http::Request;
use http_body_util::BodyExt;
use hyper::body::{Bytes, Incoming};
use serde::de::DeserializeOwned;
use std::error::Error;
pub struct Requester;
impl Requester {
/// Extracts and deserializes JSON body.
pub async fn extract_body<T>(req: Request<Incoming>) -> Result<T, Box<dyn Error + Send + Sync>>
where
T: DeserializeOwned,
{
let body = req.collect().await?.to_bytes();
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(body.to_vec())?)
}
pub async fn extract_body_bytes(
req: Request<Incoming>,
) -> Result<Bytes, Box<dyn Error + Send + Sync>> {
Ok(req.collect().await?.to_bytes())
}
}