-
-
Save rahulmr/3cf7b15ecc434e42bae64bf90795f865 to your computer and use it in GitHub Desktop.
Plugable filter for logging request body using Warp
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
| // 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