Skip to content

Instantly share code, notes, and snippets.

@rahulmr
Forked from Szymongib/warp_log_body.rs
Created April 20, 2022 11:40
Show Gist options
  • Save rahulmr/3cf7b15ecc434e42bae64bf90795f865 to your computer and use it in GitHub Desktop.
Save rahulmr/3cf7b15ecc434e42bae64bf90795f865 to your computer and use it in GitHub Desktop.
Plugable filter for logging request body using Warp
// warp version 0.2.2
use warp::{Filter, Rejection};
use bytes::{Bytes, Buf};
#[tokio::main]
async fn main() {
let port: u16 = 8080;
let api = warp::any()
.and(log_body())
.and_then(handle);
let server_future = warp::serve(api).run(([0, 0, 0, 0], port.clone()));
server_future.await;
}
fn log_body() -> impl Filter<Extract = (), Error = Rejection> + Copy {
warp::body::bytes()
.map(|b: Bytes| {
println!("Request body: {}", std::str::from_utf8(b.bytes()).expect("error converting bytes to &str"));
})
.untuple_one()
}
async fn handle() -> Result<impl warp::Reply, warp::Rejection> {
Ok(warp::reply::reply())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment