Created
December 12, 2023 18:05
-
-
Save mayakerostasia/ce191298906573c09e132043a3897cff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, Clone)] | |
pub struct BufferRequestBody(pub Bytes); | |
// we must implement `FromRequest` (and not `FromRequestParts`) to consume the body | |
#[async_trait] | |
impl<S> FromRequest<S> for BufferRequestBody | |
where | |
S: Send + Sync, | |
{ | |
type Rejection = Response<Body>; | |
async fn from_request(req: Request<Body>, state: &S) -> Result<Self, Self::Rejection> { | |
let body = Bytes::from_request(req, state) | |
.await | |
.map_err(|err| err.into_response())?; | |
Ok(Self(body)) | |
} | |
} | |
impl ToString for BufferRequestBody { | |
fn to_string(&self) -> String { | |
String::from_utf8(self.0.clone().into_iter().map(|b| b).collect::<Vec<u8>>()) | |
.unwrap_or("Failed to convert to string".to_string()) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[instrument(skip(request))] | |
async fn buffer_request_body(request: Request<Body>) -> Result<Request<Body>, Response<Body>> { | |
let req_body: BufferRequestBody = BufferRequestBody::from_request(request, &()).await?; // !!!!!! HERE !!!!!! | |
let body_clone = req_body.clone(); // !!!!!! HERE !!!!!! | |
trace!("req_body: {:?}", req_body.to_string()); | |
process_body(req_body).await.expect("Failed to do thing with request body"); | |
let request = Request::builder().body(Body::from(body_clone.0)).unwrap(); | |
Ok(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment