using layer RequestDecompressionLayer
with Router.merge
#2219
-
In the following repository, the layer It's working great. (see other question) This code is compiling:
but if I want to use the
The compilation error is the following:
In general, I find it quite difficult to fix issues with types on |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
The issue is not let other_router: Router = Router::new().route("/test", post(|| async {}));
// ^^^^^^^^ The issue is that But when you write The solution is to leave off the type annotation and do #[tokio::main]
async fn main() {
let other_router = Router::new().route("/test", post(|| async {}));
let app = Router::new()
.route("/", post(|| async {}))
.merge(other_router)
.layer(
ServiceBuilder::new()
.layer(HandleErrorLayer::new(|_: BoxError| async move {
(StatusCode::INTERNAL_SERVER_ERROR, "Unhandled server error")
}))
.layer(RequestDecompressionLayer::new()),
);
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.expect("server failed");
} Now the compiler is able to infer that the request body type must be If you need to return fn make_other_router<B>() -> Router<(), B>
where
B: axum::body::HttpBody + Send + 'static,
{
Router::new().route("/test", post(|| async {}))
} |
Beta Was this translation helpful? Give feedback.
The issue is not
merge
, its the type annotation inThe issue is that
RequestDecompressionLayer
requires a service that accepts aDecompressionBody<B>
(whereB
is some generic body type).But when you write
other_router: Router
you're setting the default body type param forRouter
which isB
. Thus you're sayingother_router: Router<(), axum::body::Body>
(()
is the default state).Router<(), axum::body::Body>
doesn't acceptDecompressionBody<B>
, it only acceptsaxum::body::Body
.The solution is to leave off the type annotation and do