Skip to content

How do I use Router::or with TraceLayer? #335

Answered by davidpdrsn
RabidFire asked this question in Q&A
Discussion options

You must be logged in to vote

The issue here is that Trace (applied by TraceLayer) changes the response body type. By default axum's response body type is axum::body::BoxBody but Trace changes that to tower_http::trace::ResponseBody. It does this because it needs to know when the body as been fully sent. You can see that in its Service implementation.

So the two routers don't have the same response body type meaning they cannot be combined. The solution is to call .boxed() on the one with the TraceLayer so .layer(TraceLayer::new_for_http()).boxed(). That changes the response body type so they now both match.

Here is a full example:

use axum::{
    handler::get,
    Router,
};
use std::net::SocketAddr;
use tower_http::t…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by davidpdrsn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants