I'm trying to implement a fairing in Rocket that logs the bodies of requests coming in. Unfortunately, since the fairing on_request
method only gets a reference to the rocket::Data
in the request, I can't call data.open()
.
Here's the fairing I have so far:
#[derive(Default)]
struct Logger {
}
impl Fairing for Logger {
fn info(&self) -> Info {
Info {
name: "Request / response logger",
kind: Kind::Request | Kind::Response
}
}
fn on_request(&self, request: &mut Request, data: &Data) {
if request.method() == Method::Post {
println!("Request came in!");
let mut dataStr = "".to_string();
data.open().read_to_string(&mut dataStr);
println!("{:?}", dataStr);
}
}
fn on_response(&self, request: &Request, response: &mut Response) {
}
}
Unsurprisingly, I get this error:
379 | data.open().read_to_string(&mut dataStr);
| ^^^^ cannot move out of borrowed content
Is there any way to log the data without consuming it? If not, is there another way to implement a logging fairing like this?