admin管理员组文章数量:1208153
I would like to read the content of rpc body. I know I need to create my own middleware. I follow the tower example from .rs
I do
#[derive(Debug, Clone)]
struct MyMiddleware<S> {
inner: S,
}
type BoxFuture<'a, T> = Pin<Box<dyn std::future::Future<Output = T> + Send + 'a>>;
impl<S, ReqBody, ResBody> Service<http::Request<ReqBody>> for MyMiddleware<S>
where
S: Service<http::Request<ReqBody>, Response = http::Response<ResBody>> + Clone + Send + 'static,
S::Future: Send + 'static,
ReqBody: Send + 'static + BodyExt,
{
type Response = S::Response;
type Error = S::Error;
type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: http::Request<ReqBody>) -> Self::Future {
// See: .Service.html#be-careful-when-cloning-inner-services
let clone = self.inner.clone();
let mut inner = std::mem::replace(&mut self.inner, clone);
Box::pin(async move {
let (parts, body) = req.into_parts();
let content = body.collect().await.unwrap().to_bytes();
println!("body : {:?}", content);
let response = inner.call(req).await?;
Ok(response)
})
}
}
But get no method named collect fouind for type parameter ReqBody in the current scope
I miss something ?
Update: I add BodyExt to ReqBody, but now, I get <ReqBody as HttpBody>::Error
doesn't implement std::fmt::Debug
.
I need to add Debug trait to ReqBody too ?
本文标签: rustget body content from rpc request before requestStack Overflow
版权声明:本文标题:rust - get body content from rpc request before request - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738710960a2108191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论